-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
33 lines (30 loc) · 872 Bytes
/
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
package zaplog
import (
"time"
"go.uber.org/zap/zapcore"
)
// GetEncoder 定义日志格式
func GetEncoder(format string) zapcore.Encoder {
// 自定义日志输出时间格式
encodeTime := func(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(t.Format("2006-01-02 15:04:05"))
}
cfg := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
TimeKey: "ts",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: encodeTime,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if format == "json" {
return zapcore.NewJSONEncoder(cfg)
}
return zapcore.NewConsoleEncoder(cfg)
}