-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
100 lines (88 loc) · 2.73 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
package logger
import (
"os"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger = nil
var sugar *zap.SugaredLogger = nil
func init() {
zapCfg := zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "lvl",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
logger, _ = zapCfg.Build(zap.AddCaller(), zap.AddCallerSkip(1))
sugar = logger.Sugar()
}
func SetLogFile(filename string) {
lumberJackLogger := &lumberjack.Logger{
Filename: filename,
MaxSize: 50,
MaxBackups: 5,
MaxAge: 7,
Compress: false,
}
writter := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger))
core := zapcore.NewCore(getEncoder(), writter, zapcore.InfoLevel)
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
sugar = logger.Sugar()
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "ts"
encoderConfig.LevelKey = "lvl"
encoderConfig.NameKey = "logger"
encoderConfig.CallerKey = "caller"
encoderConfig.FunctionKey = zapcore.OmitKey
encoderConfig.MessageKey = "msg"
encoderConfig.StacktraceKey = "stacktrace"
encoderConfig.LineEnding = zapcore.DefaultLineEnding
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeDuration = zapcore.MillisDurationEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}
// Debugf ...
func Debugf(template string, args ...interface{}) {
sugar.Debugf("\x1b[0;34m"+template+"\x1b[0m", args...)
}
// Infof ...
func Infof(template string, args ...interface{}) {
sugar.Infof("\x1b[0;32m"+template+"\x1b[0m", args...)
}
// Warnf ...
func Warnf(template string, args ...interface{}) {
sugar.Warnf("\x1b[0;35m"+template+"\x1b[0m", args...)
}
// Errorf ...
func Errorf(template string, args ...interface{}) {
sugar.Errorf("\x1b[0;31m"+template+"\x1b[0m", args...)
}
// Fatalf ...
func Fatalf(template string, args ...interface{}) {
sugar.Fatalf(template, args...)
panic("bad thing happened")
}