/
zap.go
258 lines (219 loc) · 6.59 KB
/
zap.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package log
import (
"io"
"os"
"strings"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/1024casts/snake/pkg/net/ip"
)
const (
// WriterConsole console输出
WriterConsole = "console"
// WriterFile 文件输出
WriterFile = "file"
)
const (
// RotateTimeDaily 按天切割
RotateTimeDaily = "daily"
// RotateTimeHourly 按小时切割
RotateTimeHourly = "hourly"
)
// For mapping config logger to app logger levels
var loggerLevelMap = map[string]zapcore.Level{
"debug": zapcore.DebugLevel,
"info": zapcore.InfoLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
"dpanic": zapcore.DPanicLevel,
"panic": zapcore.PanicLevel,
"fatal": zapcore.FatalLevel,
}
func getLoggerLevel(cfg *Config) zapcore.Level {
level, exist := loggerLevelMap[cfg.Level]
if !exist {
return zapcore.DebugLevel
}
return level
}
// zapLogger logger struct
type zapLogger struct {
sugarLogger *zap.SugaredLogger
}
// newZapLogger new zap logger
func newZapLogger(cfg *Config) (*zap.Logger, error) {
return buildLogger(cfg), nil
}
// newLogger new logger
func newLogger(cfg *Config) (Logger, error) {
return &zapLogger{sugarLogger: buildLogger(cfg).Sugar()}, nil
}
func buildLogger(cfg *Config) *zap.Logger {
var encoderCfg zapcore.EncoderConfig
if cfg.Development {
encoderCfg = zap.NewDevelopmentEncoderConfig()
} else {
encoderCfg = zap.NewProductionEncoderConfig()
}
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
var encoder zapcore.Encoder
if cfg.Encoding == WriterConsole {
encoder = zapcore.NewConsoleEncoder(encoderCfg)
} else {
encoder = zapcore.NewJSONEncoder(encoderCfg)
}
var cores []zapcore.Core
var options []zap.Option
// init option
hostname, _ := os.Hostname()
option := zap.Fields(
zap.String("ip", ip.GetLocalIP()),
zap.String("app_id", cfg.Name),
zap.String("instance_id", hostname),
)
options = append(options, option)
writers := strings.Split(cfg.Writers, ",")
for _, w := range writers {
switch w {
case WriterConsole:
cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), getLoggerLevel(cfg)))
case WriterFile:
// info
cores = append(cores, getInfoCore(encoder, cfg))
// warning
core, option := getWarnCore(encoder, cfg)
cores = append(cores, core)
if option != nil {
options = append(options, option)
}
// error
core, option = getErrorCore(encoder, cfg)
cores = append(cores, core)
if option != nil {
options = append(options, option)
}
default:
// console
cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), getLoggerLevel(cfg)))
// file
cores = append(cores, getAllCore(encoder, cfg))
}
}
combinedCore := zapcore.NewTee(cores...)
// 开启开发模式,堆栈跟踪
if !cfg.DisableCaller {
caller := zap.AddCaller()
options = append(options, caller)
}
// 跳过文件调用层数
addCallerSkip := zap.AddCallerSkip(2)
options = append(options, addCallerSkip)
// 构造日志
return zap.New(combinedCore, options...)
}
func getAllCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core {
allWriter := getLogWriterWithTime(cfg, cfg.LoggerFile)
allLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl <= zapcore.FatalLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(allWriter), allLevel)
}
func getInfoCore(encoder zapcore.Encoder, cfg *Config) zapcore.Core {
infoWrite := getLogWriterWithTime(cfg, cfg.LoggerFile)
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl <= zapcore.InfoLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(infoWrite), infoLevel)
}
func getWarnCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) {
warnWrite := getLogWriterWithTime(cfg, cfg.LoggerWarnFile)
var stacktrace zap.Option
warnLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
if !cfg.DisableCaller {
stacktrace = zap.AddStacktrace(zapcore.WarnLevel)
}
return lvl == zapcore.WarnLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(warnWrite), warnLevel), stacktrace
}
func getErrorCore(encoder zapcore.Encoder, cfg *Config) (zapcore.Core, zap.Option) {
errorFilename := cfg.LoggerErrorFile
errorWrite := getLogWriterWithTime(cfg, errorFilename)
var stacktrace zap.Option
errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
if !cfg.DisableCaller {
stacktrace = zap.AddStacktrace(zapcore.ErrorLevel)
}
return lvl >= zapcore.ErrorLevel
})
return zapcore.NewCore(encoder, zapcore.AddSync(errorWrite), errorLevel), stacktrace
}
// getLogWriterWithTime 按时间(小时)进行切割
func getLogWriterWithTime(cfg *Config, filename string) io.Writer {
logFullPath := filename
rotationPolicy := cfg.LogRollingPolicy
backupCount := cfg.LogBackupCount
// 默认
rotateDuration := time.Hour * 24
if rotationPolicy == RotateTimeHourly {
rotateDuration = time.Hour
}
hook, err := rotatelogs.New(
logFullPath+".%Y%m%d%H", // 时间格式使用shell的date时间格式
rotatelogs.WithLinkName(logFullPath), // 生成软链,指向最新日志文件
rotatelogs.WithRotationCount(backupCount), // 文件最大保存份数
rotatelogs.WithRotationTime(rotateDuration), // 日志切割时间间隔
)
if err != nil {
panic(err)
}
return hook
}
// Debug logger
func (l *zapLogger) Debug(args ...interface{}) {
l.sugarLogger.Debug(args...)
}
// Info logger
func (l *zapLogger) Info(args ...interface{}) {
l.sugarLogger.Info(args...)
}
// Warn logger
func (l *zapLogger) Warn(args ...interface{}) {
l.sugarLogger.Warn(args...)
}
// Error logger
func (l *zapLogger) Error(args ...interface{}) {
l.sugarLogger.Error(args...)
}
func (l *zapLogger) Fatal(args ...interface{}) {
l.sugarLogger.Fatal(args...)
}
func (l *zapLogger) Debugf(format string, args ...interface{}) {
l.sugarLogger.Debugf(format, args...)
}
func (l *zapLogger) Infof(format string, args ...interface{}) {
l.sugarLogger.Infof(format, args...)
}
func (l *zapLogger) Warnf(format string, args ...interface{}) {
l.sugarLogger.Warnf(format, args...)
}
func (l *zapLogger) Errorf(format string, args ...interface{}) {
l.sugarLogger.Errorf(format, args...)
}
func (l *zapLogger) Fatalf(format string, args ...interface{}) {
l.sugarLogger.Fatalf(format, args...)
}
func (l *zapLogger) Panicf(format string, args ...interface{}) {
l.sugarLogger.Panicf(format, args...)
}
func (l *zapLogger) WithFields(fields Fields) Logger {
var f = make([]interface{}, 0)
for k, v := range fields {
f = append(f, k)
f = append(f, v)
}
newLogger := l.sugarLogger.With(f...)
return &zapLogger{newLogger}
}