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{}
|
||||
|
||||
// WithContext returns a copy of ctx with l associated. If an instance of Logger
|
||||
// is already in the context, the context is not updated.
|
||||
// WithContext returns a copy of ctx with the receiver attached. The Logger
|
||||
// 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:
|
||||
//
|
||||
// ctx := r.Context()
|
||||
|
@ -25,13 +30,9 @@ type ctxKey struct{}
|
|||
// l.UpdateContext(func(c Context) Context {
|
||||
// return c.Str("bar", "baz")
|
||||
// })
|
||||
//
|
||||
func (l Logger) WithContext(ctx context.Context) context.Context {
|
||||
if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
||||
if lp == &l {
|
||||
// Do not store same logger.
|
||||
return ctx
|
||||
}
|
||||
} else if l.level == Disabled {
|
||||
if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
|
||||
// Do not store disabled logger.
|
||||
return ctx
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue