/
zaplog.go
290 lines (258 loc) · 6.95 KB
/
zaplog.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* @Author: calm.wu
* @Date: 2018-08-17 12:51:15
* @Last Modified by: calmwu
* @Last Modified time: 2020-08-15 20:06:10
*/
package utils
import (
"fmt"
"io"
"log"
"os"
"path"
"runtime"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
// 使用zap是因为快而不是美
var (
ZLog *zap.SugaredLogger
)
const (
_logFileMaxGSize = 100
)
func ShortCallerWithClassFunctionEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
callerPath := caller.TrimmedPath()
if f := runtime.FuncForPC(caller.PC); f != nil {
name := f.Name()
i := strings.LastIndex(name, "/")
j := strings.Index(name[i+1:], ".")
callerPath += " " + name[i+j+2:]
}
enc.AppendString(callerPath)
}
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
// CreateZapLog 创建log对象
// logFullName: dir/dir/dir/test.log
// maxSize: megabytes, default = 100
// maxAge: 多少天之后变为old file
// maxBackups: old file备份数量
// compress: old file是否压缩tgz
// logLevel: zapcore.DebugLevel
func CreateZapLog(logFullName string, maxSize, maxAge, maxBackups int, compress bool, logLevel zapcore.LevelEnabler,
callSkip int) *zap.SugaredLogger {
if maxSize > _logFileMaxGSize {
maxSize = _logFileMaxGSize
}
if maxAge < 0 {
maxAge = 0
}
if maxBackups < 0 {
maxBackups = 0
}
// 多个writer,加上stdout,在容器中可以直接logs看
writers := []zapcore.WriteSyncer{
zapcore.AddSync(&lumberjack.Logger{
Filename: logFullName,
MaxSize: maxSize, // megabytes
MaxBackups: maxBackups,
MaxAge: maxAge, // days
Compress: compress,
}),
zapcore.AddSync(os.Stdout),
}
cfg := zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
NameKey: "N",
TimeKey: "T",
CallerKey: "C",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: timeEncoder, //zapcore.ISO8601TimeEncoder,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: ShortCallerWithClassFunctionEncoder, //zapcore.ShortCallerEncoder, //
EncodeName: zapcore.FullNameEncoder,
}
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(cfg),
zapcore.NewMultiWriteSyncer(writers...),
logLevel,
)
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zap.DPanicLevel), zap.AddCallerSkip(callSkip), zap.Development())
suger := logger.Sugar()
return suger
}
// InitDefaultZapLog 初始化Zap log
func InitDefaultZapLog(logFullName string, logLevel zapcore.LevelEnabler, callSkip int) {
ZLog = CreateZapLog(logFullName, 100, 7, 7, true, logLevel, callSkip)
}
// NewSimpleLog 简化一个logger对象
func NewSimpleLog(out io.Writer) *log.Logger {
logOutput := out
if out == nil {
logOutput = os.Stdout
}
return log.New(logOutput, "", log.Ldate|log.Lmicroseconds|log.Lshortfile)
}
// Debug 封装
func Debug(args ...interface{}) {
if ZLog != nil {
ZLog.Debug(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Print(composeArgs...)
}
}
// Debugf 封装
func Debugf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Debugf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Printf(prefix+template, args...)
}
}
// Info 封装
func Info(args ...interface{}) {
if ZLog != nil {
ZLog.Info(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Print(composeArgs...)
}
}
// Infof 封装
func Infof(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Infof(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Printf(prefix+template, args...)
}
}
// Warn 封装
func Warn(args ...interface{}) {
if ZLog != nil {
ZLog.Warn(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Print(composeArgs...)
}
}
// Warnf 封装
func Warnf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Warnf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Printf(prefix+template, args...)
}
}
// Error 封装
func Error(args ...interface{}) {
if ZLog != nil {
ZLog.Error(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Print(composeArgs...)
}
}
// Errorf 封装
func Errorf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Errorf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Printf(prefix+template, args...)
}
}
// DPanic 封装
func DPanic(args ...interface{}) {
if ZLog != nil {
ZLog.DPanic(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Panic(composeArgs...)
}
}
// DPanicf 封装
func DPanicf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.DPanicf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Panicf(prefix+template, args...)
}
}
// Panic 封装
func Panic(args ...interface{}) {
if ZLog != nil {
ZLog.Panic(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Panic(composeArgs...)
}
}
// Panicf 封装
func Panicf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Panicf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Panicf(prefix+template, args...)
}
}
// Fatal 封装
func Fatal(args ...interface{}) {
if ZLog != nil {
ZLog.Fatal(args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
composeArgs := []interface{}{prefix}
composeArgs = append(composeArgs, args...)
log.Fatal(composeArgs...)
}
}
// Fatalf 封装
func Fatalf(template string, args ...interface{}) {
if ZLog != nil {
ZLog.Fatalf(template, args...)
} else {
_, file, line, _ := runtime.Caller(1)
prefix := fmt.Sprintf("%v:%v: ", path.Base(file), line)
log.Fatalf(prefix+template, args...)
}
}