-
Notifications
You must be signed in to change notification settings - Fork 52
/
log.go
67 lines (58 loc) · 1.42 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
package gn
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
var log logger = newDefaultLog()
func SetLogger(l logger) {
log = l
}
func GetLogger() logger {
return log
}
type logger interface {
Error(args ...interface{})
Info(args ...interface{})
Debug(args ...interface{})
}
type defaultLog struct {
logger *zap.SugaredLogger
}
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
func newDefaultLog() *defaultLog {
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
// Keys can be anything except the empty string.
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)),
zap.DebugLevel,
)
logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)).Sugar()
return &defaultLog{
logger: logger,
}
}
func (l *defaultLog) Error(args ...interface{}) {
l.logger.Error(args...)
}
func (l *defaultLog) Info(args ...interface{}) {
l.logger.Info(args...)
}
func (l *defaultLog) Debug(args ...interface{}) {
l.logger.Debug(args...)
}