/
ctx_log.go
95 lines (77 loc) · 1.75 KB
/
ctx_log.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
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
95
package logs
import (
"context"
)
const (
keyCtxKVs = "k_kvs"
)
type structCtxKVs struct {
kvs []any
pre *structCtxKVs
}
func CtxAddKVs(ctx context.Context, kvs ...any) context.Context {
if len(kvs) == 0 || (len(kvs)&1 == 1) {
return ctx
}
return context.WithValue(ctx, keyCtxKVs, &structCtxKVs{
kvs: kvs,
pre: getCurrentKVsStruct(ctx),
})
}
func getCurrentKVsStruct(ctx context.Context) *structCtxKVs {
if ctx == nil {
return nil
}
i := ctx.Value(keyCtxKVs)
if i == nil {
return nil
}
if kvs, ok := i.(*structCtxKVs); ok {
return kvs
}
return nil
}
func GetAllKVs(ctx context.Context) []any {
if ctx == nil {
return nil
}
kVsStruct := getCurrentKVsStruct(ctx)
if kVsStruct == nil {
return nil
}
var result []any
recursiveAllKVs(&result, kVsStruct, 0)
return result
}
func recursiveAllKVs(result *[]any, kvsStruct *structCtxKVs, total int) {
if kvsStruct == nil {
*result = make([]any, 0, total)
return
}
recursiveAllKVs(result, kvsStruct.pre, total+len(kvsStruct.kvs))
*result = append(*result, kvsStruct.kvs...)
}
func makeKVs(ctx context.Context, kvs []any) []any {
kvList := GetAllKVs(ctx)
if kvList != nil {
kvList = append(kvList, kvs...)
} else {
kvList = kvs
}
return kvList
}
func CtxDebugKvs(ctx context.Context, msg string, kvs ...any) {
logger.Debugw(msg, makeKVs(ctx, kvs)...)
}
func CtxInfoKvs(ctx context.Context, msg string, kvs ...any) {
logger.Infow(msg, makeKVs(ctx, kvs)...)
}
func CtxWarnKvs(ctx context.Context, msg string, kvs ...any) {
logger.Warnw(msg, makeKVs(ctx, kvs)...)
}
func CtxErrorKvs(ctx context.Context, msg string, kvs ...any) {
logger.Errorw(msg, makeKVs(ctx, kvs)...)
}
func CtxPanicKvs(ctx context.Context, msg string, kvs ...any) {
logger.Panicw(msg, makeKVs(ctx, kvs)...)
}