Repository navigation
Expand file tree
/
Copy pathcontext_utils.go
More file actions
94 lines (79 loc) · 2.46 KB
/
Copy pathcontext_utils.go
File metadata and controls
94 lines (79 loc) · 2.46 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package log
import (
"context"
"sync/atomic"
"testing"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
)
type logHolderKeyType struct{}
var logHolderKey = logHolderKeyType{}
type logHolder struct {
atomic.Pointer[zap.Logger]
}
// InitLogger with fields supplied (if so supplied).
// If a context already has a logger, is configuration will be lost.
func InitLogger(ctx context.Context, fields ...zap.Field) context.Context {
lh := &logHolder{}
lh.Store(zap.L().With(fields...))
ctx = context.WithValue(ctx, logHolderKey, lh)
return ctx
}
// ChildLogger _always_ creates a new logger with the fields of the parent.
// This will prevent propagation of fields higher up the stack so is useful when
// spawning child routines which may write the same field name but be meaningfully different.
func ChildLogger(ctx context.Context, fields ...zap.Field) context.Context {
lh, _ := getOrDefault(ctx)
newLogger := lh.Load().With(fields...)
lh = &logHolder{}
lh.Store(newLogger)
ctx = context.WithValue(ctx, logHolderKey, lh)
return ctx
}
// Log returns the underlying logger with its latest fields.
func Log(ctx context.Context) *zap.Logger {
lh, _ := getOrDefault(ctx)
return lh.Load()
}
// TestContext returns a context with a zaptest.Logger tied to a test object.
func TestContext(t *testing.T) context.Context {
lh := &logHolder{}
lh.Store(zaptest.NewLogger(t))
ctx, cancel := context.WithCancel(context.TODO())
t.Cleanup(cancel)
return context.WithValue(ctx, logHolderKey, lh)
}
// EnableDebug turns on debug logs for this context.
func EnableDebug(ctx context.Context) context.Context {
return SetLevel(ctx, zapcore.DebugLevel)
}
// SetLevel sets a specific log level for this context.
func SetLevel(ctx context.Context, level zapcore.Level) context.Context {
lh, ok := getOrDefault(ctx)
lh.Store(CustomLevelLogger(lh.Load(), level))
if !ok {
ctx = context.WithValue(ctx, logHolderKey, lh)
}
return ctx
}
// WithFields adds log fields to a context. These will propagate to the nearest initialized or
// child logger.
func WithFields(ctx context.Context, fields ...zap.Field) context.Context {
lh, ok := getOrDefault(ctx)
logger := lh.Load()
lh.Store(logger.With(fields...))
if !ok {
ctx = context.WithValue(ctx, logHolderKey, lh)
}
return ctx
}
func getOrDefault(ctx context.Context) (*logHolder, bool) {
lh, ok := ctx.Value(logHolderKey).(*logHolder)
if ok {
return lh, true
}
lh = &logHolder{}
lh.Store(zap.L())
return lh, false
}