/
log.go
122 lines (104 loc) · 2.88 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package log
import (
"time"
"github.com/mattn/go-colorable"
"github.com/samber/lo"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
func init() {
Init()
}
type Logger = zap.Logger
type Options struct {
IsDiscard bool // !config.Instance.Log.Enabled
IsDebug bool // config.Instance.Debug
LogFile string // config.Instance.Log.Filename
}
func Init(options ...Options) *zap.Logger {
var opts Options
if len(options) > 0 {
opts = options[0]
} else {
// default
opts = Options{
IsDiscard: false,
IsDebug: false,
LogFile: "",
}
}
if opts.IsDiscard {
zapLogger := zap.NewNop()
zap.ReplaceGlobals(zapLogger)
return zapLogger
}
core := newZapCore(zapInitParams{
logFile: opts.LogFile,
logLevel: lo.If(opts.IsDebug, zapcore.DebugLevel).Else(zapcore.InfoLevel),
maxSize: 500, // MB
maxBackups: 30, // the maximum number of old log files to retain
maxAge: 15, // the maximum number of days to retain old log files based on the
compress: true,
})
zapOpts := []zap.Option{
zap.AddCaller(),
zap.AddCallerSkip(1),
}
if opts.IsDebug {
zapOpts = append(zapOpts, zap.Development(), zap.AddStacktrace(zapcore.ErrorLevel))
}
zapLogger := zap.New(core, zapOpts...)
zap.ReplaceGlobals(zapLogger)
return zapLogger
}
func syslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006/01/02 15:04:05.000"))
}
func customCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + caller.TrimmedPath() + "]")
}
type zapInitParams struct {
logFile string
logLevel zapcore.Level
maxSize int
maxBackups int
maxAge int
compress bool
}
func newZapCore(p zapInitParams) zapcore.Core {
// set log level
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(p.logLevel)
// set encoder
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
encoderConfig.EncodeTime = syslogTimeEncoder
encoderConfig.EncodeCaller = customCallerEncoder
encoderConfig.EncodeName = zapcore.FullNameEncoder
encoderConfig.ConsoleSeparator = " "
// Log to console
methods := []zapcore.Core{
zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
zapcore.AddSync(colorable.NewColorableStdout()),
atomicLevel),
}
// Log to file
if p.logFile != "" {
fileEncoderConfig := encoderConfig
fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
fileEncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder // disable color
methods = append(methods, zapcore.NewCore(
zapcore.NewJSONEncoder(fileEncoderConfig), // write json format
zapcore.AddSync(&lumberjack.Logger{
Filename: p.logFile,
MaxSize: p.maxSize,
MaxBackups: p.maxBackups,
MaxAge: p.maxAge,
Compress: p.compress,
}),
atomicLevel))
}
return zapcore.NewTee(methods...)
}