...
1 package zerolog
2
3 import (
4 "context"
5 )
6
7 var disabledLogger *Logger
8
9 func init() {
10 SetGlobalLevel(TraceLevel)
11 l := Nop()
12 disabledLogger = &l
13 }
14
15 type ctxKey struct{}
16
17
18
19
20
21
22
23
24
25
26
27
28 func (l Logger) WithContext(ctx context.Context) context.Context {
29 if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
30 if lp == &l {
31
32 return ctx
33 }
34 } else if l.level == Disabled {
35
36 return ctx
37 }
38 return context.WithValue(ctx, ctxKey{}, &l)
39 }
40
41
42
43
44 func Ctx(ctx context.Context) *Logger {
45 if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
46 return l
47 } else if l = DefaultContextLogger; l != nil {
48 return l
49 }
50 return disabledLogger
51 }
52
View as plain text