-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx.go
More file actions
49 lines (43 loc) · 1.43 KB
/
ctx.go
File metadata and controls
49 lines (43 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package log
import (
"context"
)
// disabledLogger is a singleton disabled logger for context operations.
var disabledLogger Logger
func init() {
// Global level defaults to DebugLevel (zero value of Level = 0).
// Applications that want trace output must call SetGlobalLevel(TraceLevel).
// Do NOT reset to TraceLevel here -- it causes debug spam from dependency
// init() functions before the application configures its own log level.
disabledLogger = Noop()
}
type ctxKey struct{}
// WithContext returns a copy of ctx with the logger attached. The Logger
// attached to the provided Context (if any) will not be affected.
//
// 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()
// l := logger.Ctx(ctx)
// l.UpdateContext(func(c Context) Context {
// return c.Str("bar", "baz")
// })
func WithContext(ctx context.Context, l Logger) context.Context {
if l == nil {
l = Noop()
}
return context.WithValue(ctx, ctxKey{}, l)
}
// 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
}