Fix docs and behavior of WithContext (#499)
This commit is contained in:
parent
a9a8199d2d
commit
e3027a5732
19
ctx.go
19
ctx.go
|
@ -14,10 +14,15 @@ func init() {
|
||||||
|
|
||||||
type ctxKey struct{}
|
type ctxKey struct{}
|
||||||
|
|
||||||
// WithContext returns a copy of ctx with l associated. If an instance of Logger
|
// WithContext returns a copy of ctx with the receiver attached. The Logger
|
||||||
// is already in the context, the context is not updated.
|
// attached to the provided Context (if any) will not be effected. If the
|
||||||
|
// receiver's log level is Disabled it will only be attached to the returned
|
||||||
|
// Context if the provided Context has a previously attached Logger. If the
|
||||||
|
// provided Context has no attached Logger, a Disabled Logger will not be
|
||||||
|
// attached.
|
||||||
//
|
//
|
||||||
// For instance, to add a field to an existing logger in the context, use this
|
// Note: to modify the existing Logger attached to a Context (instead of
|
||||||
|
// replacing it in a new Context), use UpdateContext with the following
|
||||||
// notation:
|
// notation:
|
||||||
//
|
//
|
||||||
// ctx := r.Context()
|
// ctx := r.Context()
|
||||||
|
@ -25,13 +30,9 @@ type ctxKey struct{}
|
||||||
// l.UpdateContext(func(c Context) Context {
|
// l.UpdateContext(func(c Context) Context {
|
||||||
// return c.Str("bar", "baz")
|
// return c.Str("bar", "baz")
|
||||||
// })
|
// })
|
||||||
|
//
|
||||||
func (l Logger) WithContext(ctx context.Context) context.Context {
|
func (l Logger) WithContext(ctx context.Context) context.Context {
|
||||||
if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
|
||||||
if lp == &l {
|
|
||||||
// Do not store same logger.
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
} else if l.level == Disabled {
|
|
||||||
// Do not store disabled logger.
|
// Do not store disabled logger.
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue