-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
91 lines (84 loc) · 2.47 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
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
package go_toolbox
import (
"encoding/json"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"os"
"time"
)
type ZapLogger struct {
Filename string
MaxSize int
MaxBackups int
MaxAge int
Compress bool
}
// LogConsoleSeparator 分隔符
const LogConsoleSeparator string = "|-|"
var Logger *zap.Logger
func GetLogPrefix(traceId string, msgList ...string) string {
prefix := traceId
for _, msg := range msgList {
prefix = msg
}
return prefix
}
// NewZapLogger 初始化 Zap Logger
func NewZapLogger(filename string, maxSize int, maxBackups int, maxAge int, compress bool) {
// 日志滚动包
hook := lumberjack.Logger{
Filename: filename,
MaxSize: maxSize,
MaxBackups: maxBackups,
MaxAge: maxAge,
Compress: compress,
}
// 编码配置
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "linenum",
FunctionKey: "function",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
},
EncodeDuration: func(d time.Duration, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendInt64(int64(d) / 1000000)
},
EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString("\"" + caller.String() + "\"")
},
EncodeName: zapcore.FullNameEncoder,
NewReflectedEncoder: func(writer io.Writer) zapcore.ReflectedEncoder {
// 使用 github.com/goccy/go-json 代替 encoding/json
// 提升 json 解析性能
enc := json.NewEncoder(writer)
enc.SetEscapeHTML(false)
return enc
},
ConsoleSeparator: LogConsoleSeparator,
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(zap.InfoLevel)
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)), // 打印到控制台和文件
atomicLevel, // 日志级别
)
// 开启开发模式,堆栈跟踪
caller := zap.AddCaller()
// 开启文件及行号
development := zap.Development()
// 设置初始化字段
//field := zap.Fields(zap.String("serviceName", ServiceName))
// 构造日志
Logger = zap.New(core, caller, development)
}