-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
86 lines (78 loc) · 2.31 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
package sq
import (
zap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
"os"
"runtime/debug"
)
type Logger interface {
Debug(message string, keysAndValues ...interface{})
Info(message string, keysAndValues ...interface{})
Warn(message string, keysAndValues ...interface{})
Error(message string, keysAndValues ...interface{})
Sync() error
}
var Log = NewZapLogger()
type DefaultLog struct {
core *zap.SugaredLogger
}
func NewZapLogger() Logger {
// 编码
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)
core := zapcore.NewCore(
encoder,
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)),
zap.LevelEnablerFunc(func(curentLevel zapcore.Level) bool {
return true
}),
)
options := []zap.Option{
zap.AddCaller(),
// zap.AddStacktrace(zapcore.DebugLevel),
// zap.AddStacktrace(zapcore.InfoLevel),
zap.AddStacktrace(zapcore.WarnLevel),
zap.AddStacktrace(zapcore.ErrorLevel),
zap.AddCallerSkip(3),
}
return &DefaultLog{
core: zap.New(core, options...).Sugar(),
}
}
func (z DefaultLog) log(fn func(msg string, keysAndValues ...interface{}), msg string, keysAndValues ...interface{}) {
defer func() {
r := recover()
if r != nil {
log.Print(r)
debug.PrintStack()
}
}()
fn(msg, keysAndValues...)
}
func (z DefaultLog) Debug(message string, keysAndValues ...interface{}) {
z.log(z.core.Debugw, message, keysAndValues...)
}
func (z DefaultLog) Info(message string, keysAndValues ...interface{}) {
z.log(z.core.Infow, message, keysAndValues...)
}
func (z DefaultLog) Warn(message string, keysAndValues ...interface{}) {
z.log(z.core.Warnw, message, keysAndValues...)
}
func (z DefaultLog) Error(message string, keysAndValues ...interface{}) {
z.log(z.core.Errorw, message, keysAndValues...)
}
func (z DefaultLog) Panic(message string, keysAndValues ...interface{}) {
z.log(z.core.Panicw, message, keysAndValues...)
}
func (z DefaultLog) DPanic(message string, keysAndValues ...interface{}) {
z.log(z.core.DPanicw, message, keysAndValues...)
}
func (z DefaultLog) Fatal(message string, keysAndValues ...interface{}) {
z.log(z.core.Fatalw, message, keysAndValues...)
}
func (z DefaultLog) Sync() error {
return z.core.Sync()
}