-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
68 lines (52 loc) · 1.28 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
60
61
62
63
64
65
66
67
68
package log
import (
"context"
)
type logTag struct{}
type logMoreInfo struct{}
// Info 代表一个 k=v 键值对。
type Info struct {
Key string
Value interface{}
}
type moreInfo struct {
infoList []Info
}
var (
keyLogTag logTag
keyLogMoreInfo logMoreInfo
)
// WithTag 在 ctx 里面存一个日志 tag 信息,用于日志输出。
func WithTag(ctx context.Context, tag string) context.Context {
return context.WithValue(ctx, keyLogTag, tag)
}
func tag(ctx context.Context) string {
v := ctx.Value(keyLogTag)
if v == nil {
return ""
}
return v.(string)
}
// WithMoreInfo 在 ctx 里面保存更多的信息,可以自动在输出 log 时候将这些信息以 k=v 形式输出。
func WithMoreInfo(ctx context.Context, info ...Info) context.Context {
if len(info) == 0 {
return ctx
}
var infoList []Info
if more := ctx.Value(keyLogMoreInfo); more != nil {
old := more.(moreInfo)
infoList = make([]Info, 0, len(old.infoList)+len(info))
infoList = append(infoList, old.infoList...)
}
infoList = append(infoList, info...)
return context.WithValue(ctx, keyLogMoreInfo, moreInfo{
infoList: infoList,
})
}
func findMoreInfo(ctx context.Context) []Info {
more := ctx.Value(keyLogMoreInfo)
if more == nil {
return nil
}
return more.(moreInfo).infoList
}