-
Notifications
You must be signed in to change notification settings - Fork 1
/
zap.go
119 lines (103 loc) · 2.67 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
package wzap
import (
"fmt"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var defaultDir string
var setDefaultDir bool
// SetDefaultDir sets default directory of all zap writer logger.
func SetDefaultDir(dir string) {
defaultDir = dir
setDefaultDir = true
}
// DefaultDir gets defaultDir.
func DefaultDir() string {
return defaultDir
}
// NewZapWriter constructs a new ZapWriter instance.
func NewZapWriter(path string, level int, fs []Field) *ZapWriter {
l := &ZapWriter{
level: level,
}
encoderConfig := zap.NewProductionConfig().EncoderConfig
encoderConfig.LevelKey = "lv"
encoderConfig.StacktraceKey = "stack"
enc := zapcore.NewJSONEncoder(encoderConfig)
filePath := path
if setDefaultDir {
filePath = fmt.Sprintf("%s/%s", defaultDir, path)
}
syncer := zapcore.AddSync(&lumberjack.Logger{
Filename: filePath,
MaxSize: 500, // MB
MaxAge: 1, // days
MaxBackups: 100,
LocalTime: true,
Compress: true,
})
core := zapcore.NewCore(
enc,
syncer,
zap.DebugLevel,
)
opts := []zap.Option{
zap.AddStacktrace(zap.ErrorLevel),
zap.Fields(fs...),
}
l.logger = zap.New(core, opts...).Sugar()
return l
}
// A ZapWriter wraps the zap.SugarWriter.
type ZapWriter struct {
logger *zap.SugaredLogger
options Options
level int
}
// Sync flushes any buffered log entries.
func (l *ZapWriter) Sync() error {
return l.logger.Sync()
}
// Level return ZapWriter lever.
func (l *ZapWriter) Level() int {
return l.level
}
// Print logs message with structured-style.
func (l *ZapWriter) Print(level int, msg string, keysAndValues ...interface{}) {
switch level {
case FatalLevel:
l.logger.Fatalw(msg, keysAndValues...)
case PanicLevel:
l.logger.Panicw(msg, keysAndValues...)
case ErrorLevel:
l.logger.Errorw(msg, keysAndValues...)
case WarnLevel:
l.logger.Warnw(msg, keysAndValues...)
case InfoLevel:
l.logger.Infow(msg, keysAndValues...)
case DebugLevel:
l.logger.Debugw(msg, keysAndValues...)
}
}
// Printf logs message with printf-style.
func (l *ZapWriter) Printf(level int, format string, keysAndValues ...interface{}) {
switch level {
case FatalLevel:
l.logger.Fatalf(format, keysAndValues...)
case PanicLevel:
l.logger.Panicf(format, keysAndValues...)
case ErrorLevel:
l.logger.Errorf(format, keysAndValues...)
case WarnLevel:
l.logger.Warnf(format, keysAndValues...)
case InfoLevel:
l.logger.Infof(format, keysAndValues...)
case DebugLevel:
l.logger.Debugf(format, keysAndValues...)
}
}
// CheckErr checks error, error will be logged if it's not equal to nil.
func (l *ZapWriter) CheckErr(err error, logFunc func(string, ...interface{})) (isErr bool) {
return checkErr(err, logFunc)
}