-
Notifications
You must be signed in to change notification settings - Fork 13
/
encoder.go
37 lines (34 loc) · 1.15 KB
/
encoder.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
package main
import (
"github.com/axiaoxin-com/logging"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
/* custom logger encoder */
options := logging.Options{
Name: "apiserver",
EncoderConfig: &zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Logger",
CallerKey: "Caller",
MessageKey: "Message",
StacktraceKey: "Stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: logging.TimeEncoder, // 使用 logging 的 time 格式
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: logging.CallerEncoder, // 使用 logging 的 caller 格式
},
DisableCaller: false,
}
logger, _ := logging.NewLogger(options)
logger.Debug("EncoderConfig Debug", zap.Reflect("Tags", map[string]interface{}{
"Status": "200 OK",
"StatusCode": 200,
"Latency": 0.075,
}))
// Output:
// {"Level":"DEBUG","Time":"2020-04-15 19:23:44.373302","Logger":"apiserver","Caller":"example/encoder.go:main:30","Message":"EncoderConfig Debug","pid":66937,"Tags":{"Latency":0.075,"Status":"200 OK","StatusCode":200}}
}