/
kitex_log.go
125 lines (122 loc) · 4.03 KB
/
kitex_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package logutils
import (
Kitexzap "github.com/baoyxing/micro-extend/pkg/utils/logutils/kitex"
"github.com/cloudwego/kitex/pkg/klog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
func NewKitexLog(opts ...Option) {
config := DefaultOptionConfig()
for _, opt := range opts {
opt.apply(config)
}
dynamicLevel := zap.NewAtomicLevel()
dynamicLevel.SetLevel(zap.DebugLevel)
coreConfigs := make([]Kitexzap.CoreConfig, 0)
switch config.outputMode {
case 1:
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewConsoleEncoder(pkgEncoderConfig()),
Ws: zapcore.AddSync(os.Stdout),
Lvl: dynamicLevel,
})
case 2:
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/all/app", config),
Lvl: zap.NewAtomicLevelAt(zapcore.DebugLevel),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/debug/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.DebugLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/info/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.InfoLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/warn/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.WarnLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/error/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.ErrorLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/panic/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev >= zap.DPanicLevel
}),
})
case 3:
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewConsoleEncoder(pkgEncoderConfig()),
Ws: zapcore.AddSync(os.Stdout),
Lvl: dynamicLevel,
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/all/app", config),
Lvl: zap.NewAtomicLevelAt(zapcore.DebugLevel),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/debug/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.DebugLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/info/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.InfoLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/warn/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.WarnLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/error/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == zap.ErrorLevel
}),
})
coreConfigs = append(coreConfigs, Kitexzap.CoreConfig{
Enc: zapcore.NewJSONEncoder(pkgEncoderConfig()),
Ws: NewWriteSyncer("/panic/app", config),
Lvl: zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev >= zap.DPanicLevel
}),
})
}
logger := Kitexzap.NewLogger(Kitexzap.WithCores(coreConfigs...),
Kitexzap.WithZapOptions(zap.AddCaller(), zap.AddCallerSkip(3)),
)
defer func(logger *Kitexzap.Logger) {
err := logger.Sync()
if err != nil {
logger.Error(err)
}
}(logger)
klog.SetLogger(logger)
}