This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
/
log.go
72 lines (62 loc) · 1.85 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
package log
import (
"io"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
instance *zap.SugaredLogger
output zapcore.WriteSyncer
defaultEncoderConfig = zapcore.EncoderConfig{
TimeKey: "TIME",
LevelKey: "LEVEL",
NameKey: "LOGGER",
CallerKey: "LINE",
MessageKey: "MESSAGE",
StacktraceKey: "STACKTRACE",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder, // 大写编码器,debug 模式时带颜色
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
)
// Init log instance
func Init(appName, logPath string, debug bool) {
ws := []zapcore.WriteSyncer{zapcore.AddSync(os.Stdout)}
if logPath := logPath; logPath != "" {
ws = append(ws, zapcore.AddSync(&lumberjack.Logger{
Filename: logPath,
MaxSize: 500,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
}))
}
output = zapcore.NewMultiWriteSyncer(ws...)
encoder := zapcore.NewConsoleEncoder(defaultEncoderConfig)
level := zap.DebugLevel
if !debug {
defaultEncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
level = zap.InfoLevel
encoder = zapcore.NewJSONEncoder(defaultEncoderConfig)
}
instance = zap.New(zapcore.NewCore(encoder, output, level), zap.AddCaller()).Sugar().Named(appName)
initPrinter(instance)
}
// Close log sync and remove instance
func Close() {
_ = instance.Sync()
instance = nil
}
// ShowLine Line number display switch
func ShowLine(enable bool) *zap.SugaredLogger {
return instance.Desugar().WithOptions(zap.WithCaller(enable)).Sugar()
}
// Output return a log io.Writer
func Output() io.Writer {
return output
}