-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
105 lines (81 loc) · 2.21 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package logger
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var zapLog *zap.SugaredLogger
var logLevelSeverity = map[zapcore.Level]string{
zapcore.DebugLevel: "DEBUG",
zapcore.InfoLevel: "INFO",
zapcore.WarnLevel: "WARNING",
zapcore.ErrorLevel: "ERROR",
zapcore.DPanicLevel: "CRITICAL",
zapcore.PanicLevel: "CRITICAL",
zapcore.FatalLevel: "CRITICAL",
}
func init() {
highPriority := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
return lv >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
stdoutSink := zapcore.Lock(os.Stdout)
stderrSink := zapcore.Lock(os.Stderr)
enc := zapcore.NewConsoleEncoder(newEncoderConfig())
core := zapcore.NewTee(
zapcore.NewCore(enc, stderrSink, highPriority),
zapcore.NewCore(enc, stdoutSink, lowPriority),
)
l := zap.New(core, zap.AddCallerSkip(1))
zapLog = l.Sugar()
}
func EncodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(logLevelSeverity[l])
}
func newEncoderConfig() zapcore.EncoderConfig {
cfg := zap.NewProductionEncoderConfig()
cfg.EncodeLevel = EncodeLevel
cfg.EncodeTime = zapcore.ISO8601TimeEncoder
return cfg
}
func Debug(args ...interface{}) {
zapLog.Debug(args...)
}
func Debugf(template string, args ...interface{}) {
zapLog.Debugf(template, args...)
}
func Debugw(msg string, keysAndValues ...interface{}) {
zapLog.Debugw(msg, keysAndValues...)
}
func Info(args ...interface{}) {
zapLog.Info(args...)
}
func Infof(template string, args ...interface{}) {
zapLog.Infof(template, args...)
}
func Infow(msg string, keysAndValues ...interface{}) {
zapLog.Infow(msg, keysAndValues...)
}
func Error(args ...interface{}) {
zapLog.Error(args...)
}
func Errorf(template string, args ...interface{}) {
zapLog.Errorf(template, args...)
}
func Errorw(msg string, keysAndValues ...interface{}) {
zapLog.Errorw(msg, keysAndValues...)
}
func Fatal(args ...interface{}) {
zapLog.Fatal(args...)
}
func Fatalf(template string, args ...interface{}) {
zapLog.Fatalf(template, args...)
}
func Fatalw(msg string, keysAndValues ...interface{}) {
zapLog.Fatalw(msg, keysAndValues...)
}
func Sync() error {
return zapLog.Sync()
}