2021-09-20 09:28:00 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var disabledLogger *Logger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetGlobalLevel(TraceLevel)
|
|
|
|
l := Nop()
|
|
|
|
disabledLogger = &l
|
|
|
|
}
|
|
|
|
|
|
|
|
type ctxKey struct{}
|
|
|
|
|
2024-08-23 23:15:31 +00:00
|
|
|
// 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.
|
2021-09-20 09:28:00 +00:00
|
|
|
//
|
2024-08-23 23:15:31 +00:00
|
|
|
// Note: to modify the existing Logger attached to a Context (instead of
|
|
|
|
// replacing it in a new Context), use UpdateContext with the following
|
2021-09-20 09:28:00 +00:00
|
|
|
// notation:
|
|
|
|
//
|
|
|
|
// ctx := r.Context()
|
|
|
|
// l := zerolog.Ctx(ctx)
|
|
|
|
// l.UpdateContext(func(c Context) Context {
|
|
|
|
// return c.Str("bar", "baz")
|
|
|
|
// })
|
2024-08-23 23:15:31 +00:00
|
|
|
//
|
|
|
|
func (l Logger) WithContext(ctx context.Context) context.Context {
|
|
|
|
if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
|
2021-09-20 09:28:00 +00:00
|
|
|
// Do not store disabled logger.
|
|
|
|
return ctx
|
|
|
|
}
|
2024-08-23 23:15:31 +00:00
|
|
|
return context.WithValue(ctx, ctxKey{}, &l)
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ctx returns the Logger associated with the ctx. If no logger
|
|
|
|
// is associated, DefaultContextLogger is returned, unless DefaultContextLogger
|
|
|
|
// is nil, in which case a disabled logger is returned.
|
|
|
|
func Ctx(ctx context.Context) *Logger {
|
|
|
|
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
|
|
|
return l
|
|
|
|
} else if l = DefaultContextLogger; l != nil {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
return disabledLogger
|
|
|
|
}
|