-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
39 lines (35 loc) · 1.06 KB
/
logger.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
package logutils
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap/zapcore"
)
func NewLogger(opts ...Option) *lumberjack.Logger {
config := DefaulutLumberjackConfig()
for _, opt := range opts {
opt.apply(config)
}
return &lumberjack.Logger{
Filename: config.filename,
MaxSize: config.maxSize,
MaxAge: config.maxAge,
MaxBackups: config.maxBackups,
LocalTime: config.localTime,
Compress: config.compress,
}
}
func NewZapEncoderConfig() zapcore.Encoder {
return zapcore.NewJSONEncoder(zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
})
}