/
log.go
373 lines (306 loc) · 10 KB
/
log.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package log
import (
"fmt"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger represents the ability to log messages, both errcode and not.
type Logger interface {
// info level
Info(msg string, fields ...Field)
Infof(format string, v ...interface{})
Infow(msg string, keysAndValues ...interface{})
// debug level
Debug(msg string, fields ...Field)
Debugf(format string, v ...interface{})
Debugw(msg string, keysAndValues ...interface{})
// warn level
Warn(msg string, fields ...Field)
Warnf(format string, v ...interface{})
Warnw(msg string, keysAndValues ...interface{})
// error level
Error(msg string, fields ...Field)
Errorf(format string, v ...interface{})
Errorw(msg string, keysAndValues ...interface{})
ErrorR(format string, v ...interface{}) error
// panic level
Panic(msg string, fields ...Field)
Panicf(format string, v ...interface{})
Panicw(msg string, keysAndValues ...interface{})
// fatal level
Fatal(msg string, fields ...Field)
Fatalf(format string, v ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
// WithValues adds some key-value pairs of context to a logger.
WithValues(keysAndValues ...interface{}) Logger
// WithName adds a new element to the logger's name.
// Successive calls with WithName continue to append
// suffixes to the logger's name. It's strongly recommended
// that name segments contain only letters, digits, and hyphens
// (see the package documentation for more information).
WithName(name string) Logger
// Flush calls the underlying Core's Sync method, flushing any buffered
// log entries. Applications should take care to call Sync before exiting.
Flush()
}
type ZapLogger struct {
zapLogger *zap.Logger
level zapcore.Level
}
// wrap interface value with zap.Any for performance
func handleFields(l *zap.Logger, args []interface{}, additional ...zap.Field) []zap.Field {
if len(args) == 0 {
return additional
}
fields := make([]zap.Field, 0, len(args)/2+len(additional))
for i := 0; i < len(args); {
if _, ok := args[i].(zap.Field); ok {
l.DPanic("strongly-typed Zap Field passed to log", zap.Any("zap field", args[i]))
break
}
key, val := args[i], args[i+1]
keyStr, isString := key.(string)
if !isString {
l.DPanic(
"non-string key argument passed to logging, ignoring all later arguments",
zap.Any("invalid key", key),
)
break
}
fields = append(fields, zap.Any(keyStr, val))
i += 2
}
return append(fields, additional...)
}
var (
std = New(NewOptions())
mu sync.Mutex
)
// init logger. level should be one of "debug", "info", "warn", "error", "panic", "fatal", format should be oneof "console","json"
func Init(level, format string) {
mu.Lock()
defer mu.Unlock()
options := NewOptions()
options.Level = level
options.Format = format
err := options.Validate()
if err != nil {
panic(err)
}
std = New(options)
}
// New create logger by opts which can customized by command arguments.
func New(opts *Options) *ZapLogger {
if opts == nil {
opts = NewOptions()
}
var zapLevel zapcore.Level
if err := zapLevel.UnmarshalText([]byte(opts.Level)); err != nil {
zapLevel = zapcore.InfoLevel
}
encodeLevel := zapcore.CapitalLevelEncoder
// when output to local path, with color is forbidden
if opts.Format == consoleFormat && opts.EnableColor {
encodeLevel = zapcore.CapitalColorLevelEncoder
}
encoderConfig := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "timestamp",
NameKey: "logger",
CallerKey: "caller",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: encodeLevel,
EncodeTime: timeEncoder,
EncodeDuration: milliSecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
loggerConfig := &zap.Config{
Level: zap.NewAtomicLevelAt(zapLevel),
Development: opts.Development,
DisableCaller: opts.DisableCaller,
DisableStacktrace: opts.DisableStacktrace,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: opts.Format,
EncoderConfig: encoderConfig,
OutputPaths: opts.OutputPaths,
ErrorOutputPaths: opts.ErrorOutputPaths,
}
var err error
l, err := loggerConfig.Build(zap.AddStacktrace(zapcore.PanicLevel), zap.AddCallerSkip(1))
if err != nil {
panic(err)
}
logger := &ZapLogger{
zapLogger: l.Named(opts.Name),
level: loggerConfig.Level.Level(),
}
return logger
}
// WithValues creates a child logger and adds adds Zap fields to it.
func WithValues(keysAndValues ...interface{}) Logger { return std.WithValues(keysAndValues...) }
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
newLogger := l.zapLogger.With(handleFields(l.zapLogger, keysAndValues)...)
return &ZapLogger{
zapLogger: newLogger,
}
}
// WithName adds a new path segment to the logger's name. Segments are joined by
// periods. By default, Loggers are unnamed.
func WithName(s string) Logger { return std.WithName(s) }
func (l *ZapLogger) WithName(name string) Logger {
newLogger := l.zapLogger.Named(name)
return &ZapLogger{
zapLogger: newLogger,
}
}
// Flush calls the underlying Core's Sync method, flushing any buffered
// log entries. Applications should take care to call Sync before exiting.
func Flush() { std.Flush() }
func (l *ZapLogger) Flush() {
_ = l.zapLogger.Sync()
}
// Debug method output debug level log.
func Debug(msg string, fields ...Field) {
std.zapLogger.Debug(msg, fields...)
}
func (l *ZapLogger) Debug(msg string, fields ...Field) {
l.zapLogger.Debug(msg, fields...)
}
// Debugf method output debug level log.
func Debugf(format string, v ...interface{}) {
std.zapLogger.Sugar().Debugf(format, v...)
}
func (l *ZapLogger) Debugf(format string, v ...interface{}) {
l.zapLogger.Sugar().Debugf(format, v...)
}
// Debugw method output debug level log.
func Debugw(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Debugw(msg, keysAndValues...)
}
func (l *ZapLogger) Debugw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Debugw(msg, keysAndValues...)
}
// Info method output info level log.
func Info(msg string, fields ...Field) {
std.zapLogger.Info(msg, fields...)
}
func (l *ZapLogger) Info(msg string, fields ...Field) {
l.zapLogger.Info(msg, fields...)
}
// Infof method output info level log.
func Infof(format string, v ...interface{}) {
std.zapLogger.Sugar().Infof(format, v...)
}
func (l *ZapLogger) Infof(format string, v ...interface{}) {
l.zapLogger.Sugar().Infof(format, v...)
}
// Infow method output info level log.
func Infow(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Infow(msg, keysAndValues...)
}
func (l *ZapLogger) Infow(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Infow(msg, keysAndValues...)
}
// Warn method output warning level log.
func Warn(msg string, fields ...Field) {
std.zapLogger.Warn(msg, fields...)
}
func (l *ZapLogger) Warn(msg string, fields ...Field) {
l.zapLogger.Warn(msg, fields...)
}
// Warnf method output warning level log.
func Warnf(format string, v ...interface{}) {
std.zapLogger.Sugar().Warnf(format, v...)
}
func (l *ZapLogger) Warnf(format string, v ...interface{}) {
l.zapLogger.Sugar().Warnf(format, v...)
}
// Warnw method output warning level log.
func Warnw(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Warnw(msg, keysAndValues...)
}
func (l *ZapLogger) Warnw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Warnw(msg, keysAndValues...)
}
// Error method output error level log.
func Error(msg string, fields ...Field) {
std.zapLogger.Error(msg, fields...)
}
func (l *ZapLogger) Error(msg string, fields ...Field) {
l.zapLogger.Error(msg, fields...)
}
// Errorf method output error level log.
func Errorf(format string, v ...interface{}) {
std.zapLogger.Sugar().Errorf(format, v...)
}
func (l *ZapLogger) Errorf(format string, v ...interface{}) {
l.zapLogger.Sugar().Errorf(format, v...)
}
func ErrorR(format string, v ...interface{}) error {
std.zapLogger.Sugar().Errorf(format, v...)
return fmt.Errorf(format, v...)
}
func (l *ZapLogger) ErrorR(format string, v ...interface{}) error {
l.zapLogger.Sugar().Errorf(format, v...)
return fmt.Errorf(format, v...)
}
// Errorw method output error level log.
func Errorw(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Errorw(msg, keysAndValues...)
}
func (l *ZapLogger) Errorw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Errorw(msg, keysAndValues...)
}
// Panic method output panic level log and shutdown application.
func Panic(msg string, fields ...Field) {
std.zapLogger.Panic(msg, fields...)
}
func (l *ZapLogger) Panic(msg string, fields ...Field) {
l.zapLogger.Panic(msg, fields...)
}
// Panicf method output panic level log and shutdown application.
func Panicf(format string, v ...interface{}) {
std.zapLogger.Sugar().Panicf(format, v...)
}
func (l *ZapLogger) Panicf(format string, v ...interface{}) {
l.zapLogger.Sugar().Panicf(format, v...)
}
// Panicw method output panic level log.
func Panicw(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Panicw(msg, keysAndValues...)
}
func (l *ZapLogger) Panicw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Panicw(msg, keysAndValues...)
}
// Fatal method output fatal level log.
func Fatal(msg string, fields ...Field) {
std.zapLogger.Fatal(msg, fields...)
}
func (l *ZapLogger) Fatal(msg string, fields ...Field) {
l.zapLogger.Fatal(msg, fields...)
}
// Fatalf method output fatal level log.
func Fatalf(format string, v ...interface{}) {
std.zapLogger.Sugar().Fatalf(format, v...)
}
func (l *ZapLogger) Fatalf(format string, v ...interface{}) {
l.zapLogger.Sugar().Fatalf(format, v...)
}
// Fatalw method output Fatalw level log.
func Fatalw(msg string, keysAndValues ...interface{}) {
std.zapLogger.Sugar().Fatalw(msg, keysAndValues...)
}
func (l *ZapLogger) Fatalw(msg string, keysAndValues ...interface{}) {
l.zapLogger.Sugar().Fatalw(msg, keysAndValues...)
}
// //nolint:predeclared
// func (l *ZapLogger) clone() *ZapLogger {
// copy := *l
// return ©
// }