2017-05-19 19:59:10 +00:00
|
|
|
package zerolog
|
|
|
|
|
2017-05-20 07:22:37 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var disabledLogger = New(ioutil.Discard).Level(Disabled)
|
2017-05-19 19:59:10 +00:00
|
|
|
|
|
|
|
type ctxKey struct{}
|
|
|
|
|
|
|
|
// WithContext returns a copy of ctx with l associated.
|
|
|
|
func (l Logger) WithContext(ctx context.Context) context.Context {
|
2017-05-20 08:48:00 +00:00
|
|
|
if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
|
|
|
// Update existing pointer.
|
|
|
|
*lp = l
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, ctxKey{}, &l)
|
2017-05-19 19:59:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 07:22:37 +00:00
|
|
|
// Ctx returns the Logger associated with the ctx. If no logger
|
|
|
|
// is associated, a disabled logger is returned.
|
|
|
|
func Ctx(ctx context.Context) Logger {
|
2017-05-20 08:48:00 +00:00
|
|
|
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
|
|
|
return *l
|
2017-05-20 07:22:37 +00:00
|
|
|
}
|
|
|
|
return disabledLogger
|
2017-05-19 19:59:10 +00:00
|
|
|
}
|