This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
context.go
59 lines (50 loc) · 1.66 KB
/
context.go
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
package ctxlog
import (
"context"
"go.uber.org/zap"
"k8s.io/client-go/tools/record"
)
type ctxLogger struct{}
type ctxRecorder struct{}
// key must be comparable and should not be of type string
var (
ctxLoggerKey = &ctxLogger{}
ctxRecorderKey = &ctxRecorder{}
nopLogger = zap.NewNop().Sugar()
nopRecorder = record.FakeRecorder{}
)
// NewParentContext returns a new context with a logger
func NewParentContext(log *zap.SugaredLogger) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, ctxLoggerKey, log)
return ctx
}
// NewContextWithRecorder returns a new child context with the named
// recorder and log inside
func NewContextWithRecorder(ctx context.Context, name string, recorder record.EventRecorder) context.Context {
ctx = context.WithValue(ctx, ctxRecorderKey, recorder)
log := ExtractLogger(ctx)
log = log.Named(name)
return context.WithValue(ctx, ctxLoggerKey, log)
}
// ExtractLoggerWithOptions returns a logger with different options than the parent
func ExtractLoggerWithOptions(ctx context.Context, options ...zap.Option) *zap.SugaredLogger {
log := ExtractLogger(ctx)
return log.Desugar().WithOptions(options...).Sugar()
}
// ExtractLogger returns the logger from the context
func ExtractLogger(ctx context.Context) *zap.SugaredLogger {
log, ok := ctx.Value(ctxLoggerKey).(*zap.SugaredLogger)
if !ok || log == nil {
return nopLogger
}
return log
}
// ExtractRecorder returns the event recorder from the context
func ExtractRecorder(ctx context.Context) record.EventRecorder {
recorder, ok := ctx.Value(ctxRecorderKey).(record.EventRecorder)
if !ok || recorder == nil {
return &nopRecorder
}
return recorder
}