forked from BioinfoUNIBA/REDItools2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
68 lines (58 loc) · 1.42 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
package main
import (
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
// NewEncoderConfig as name says
func NewEncoderConfig() zapcore.EncoderConfig {
return 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.CapitalColorLevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
// TimeEncoder as name says
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
func setLogger(debug bool, log string) {
level := zap.InfoLevel
encoder := NewEncoderConfig()
if debug {
level = zap.DebugLevel
encoder = zap.NewDevelopmentEncoderConfig()
}
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoder),
zapcore.AddSync(os.Stderr),
level,
)
if log != "" {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: log,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
core = zapcore.NewCore(
zapcore.NewJSONEncoder(encoder),
zapcore.AddSync(w),
level,
)
}
logger = zap.New(core, zap.AddCaller())
defer logger.Sync()
sugar = logger.Sugar()
}