-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
57 lines (44 loc) · 1.36 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
package log
import (
"context"
"github.com/SyntSugar/ss-infra-go/consts"
"github.com/SyntSugar/ss-infra-go/tracing"
"go.uber.org/zap"
)
func AppendContextFields(parent context.Context, fields ...zap.Field) context.Context {
if parent == nil {
return nil
}
// The value-passing method is used here, so that the subclass function sets new fields,
// which will not affect the fields set by the caller, causing some unpredictable behavior
oldFields, ok := parent.Value(ctxLogger).([]zap.Field)
if ok {
fields = append(fields, oldFields...)
}
//nolint:staticcheck,SA1029
ctx := context.WithValue(parent, ctxLogger, fields)
return ctx
}
func GetContextFields(ctx context.Context) []zap.Field {
if ctx == nil {
return nil
}
fields, ok := ctx.Value(ctxLogger).([]zap.Field)
if !ok {
return nil
}
if traceID := tracing.GetAmTraceID(ctx); traceID != "" {
fields = append(fields, zap.String(consts.KeyAMTraceID, traceID))
}
if cloudflareRayID := tracing.GetCloudflareRayID(ctx); cloudflareRayID != "" {
fields = append(fields, zap.String(consts.KeyCloudflareRay, cloudflareRayID))
}
return fields
}
// DynamicDebugLogging open the debug level logging in dynamically
func DynamicDebugLogging(parent context.Context) context.Context {
if parent == nil {
return parent
}
return context.WithValue(parent, consts.ContextKeyEnableDebugLogging, true)
}