/
log.go
237 lines (198 loc) · 5.91 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package repository
import (
"os"
"sync"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// for sake of performance, no lock here
// should avoid calling log in package init func
var (
log *zap.SugaredLogger
once sync.Once
)
var levelMap = map[string]zapcore.Level{
"debug": zapcore.DebugLevel,
"info": zapcore.InfoLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
"dpanic": zapcore.DPanicLevel,
"panic": zapcore.PanicLevel,
"fatal": zapcore.FatalLevel,
}
func getLoggerLevel(lvl string) zapcore.Level {
if level, ok := levelMap[lvl]; ok {
return level
}
return zapcore.InfoLevel
}
func Global() *zap.SugaredLogger {
return log
}
// Init should be called firstly in main
// Only the first Init call can init the logger, and the later calls will be ignored
func Init(fn string, level string, app string) {
once.Do(func() { initHelper(fn, level, app) })
}
func initHelper(fn string, level string, app string) {
// set default log file name
if fn == "" {
fn = "data-debug.log"
}
hook := &lumberjack.Logger{
Filename: fn,
MaxSize: 25, // 20m
MaxBackups: 4,
MaxAge: 7, // 7days
LocalTime: true,
Compress: true,
}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "file",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(hook)), // 打印到控制台和文件
zap.NewAtomicLevelAt(getLoggerLevel(level)), // 日志级别
)
logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)).With(zap.String("app", app))
log = logger.Sugar()
}
// Debug
func Debug(args ...interface{}) {
log.Debug(args...)
}
// Debugf
func Debugf(template string, args ...interface{}) {
log.Debugf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Debugw(msg string, kvs ...interface{}) {
log.Debugw(msg, kvs)
}
// Info
func Info(args ...interface{}) {
log.Info(args...)
}
// Infof
func Infof(template string, args ...interface{}) {
log.Infof(template, args...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Infow(msg string, kvs ...interface{}) {
log.Infow(msg, kvs)
}
// Warn
func Warn(args ...interface{}) {
log.Warn(args...)
}
// Warnf
func Warnf(template string, args ...interface{}) {
log.Warnf(template, args...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Warnw(msg string, kvs ...interface{}) {
log.Warnw(msg, kvs)
}
// Error
func Error(args ...interface{}) {
log.Error(args...)
}
// Errorf
func Errorf(template string, args ...interface{}) {
log.Errorf(template, args...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Errorw(msg string, kvs ...interface{}) {
log.Errorw(msg, kvs)
}
// DPanic
func DPanic(args ...interface{}) {
log.DPanic(args...)
}
// DPanic
func DPanicf(template string, args ...interface{}) {
log.DPanicf(template, args...)
}
// DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With.
func DPanicw(msg string, kvs ...interface{}) {
log.DPanicw(msg, kvs)
}
// Panic
func Panic(args ...interface{}) {
log.Panic(args...)
}
// Panicf
func Panicf(template string, args ...interface{}) {
log.Panicf(template, args...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func Panicw(msg string, kvs ...interface{}) {
log.Panicw(msg, kvs)
}
// Fatal
func Fatal(args ...interface{}) {
log.Fatal(args...)
}
// Fatalf
func Fatalf(template string, args ...interface{}) {
log.Fatalf(template, args...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func Fatalw(msg string, kvs ...interface{}) {
log.Fatalw(msg, kvs)
}
func init() {
initHelper("start.log", "debug", "")
}
// Sync flushes any buffered log entries.
func Sync() error {
return log.Sync()
}
// New sugar logger
func New(app string, level zapcore.Level) *zap.SugaredLogger {
if level < zap.DebugLevel || level > zap.FatalLevel {
level = zap.ErrorLevel
}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "file",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), // 打印到控制台
zap.NewAtomicLevelAt(level), // 日志级别
)
l := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(0)).With(zap.String("app", app))
return l.Sugar()
}