mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-23 06:40:23 +00:00
28 lines
676 B
Go
28 lines
676 B
Go
|
package flume
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
)
|
||
|
|
||
|
// DefaultLogger is returned by FromContext if no other logger has been
|
||
|
// injected into the context.
|
||
|
var DefaultLogger = New("")
|
||
|
|
||
|
type ctxKey struct{}
|
||
|
|
||
|
var loggerKey = &ctxKey{}
|
||
|
|
||
|
// WithLogger returns a new context with the specified logger injected into it.
|
||
|
func WithLogger(ctx context.Context, l Logger) context.Context {
|
||
|
return context.WithValue(ctx, loggerKey, l)
|
||
|
}
|
||
|
|
||
|
// FromContext returns a logger from the context. If the context
|
||
|
// doesn't contain a logger, the DefaultLogger will be returned.
|
||
|
func FromContext(ctx context.Context) Logger {
|
||
|
if l, ok := ctx.Value(loggerKey).(Logger); ok {
|
||
|
return l
|
||
|
}
|
||
|
return DefaultLogger
|
||
|
}
|