-
Notifications
You must be signed in to change notification settings - Fork 48
/
log.go
96 lines (82 loc) · 2.54 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
package bootstrap
import (
"github.com/jassue/jassue-gin/global"
"github.com/jassue/jassue-gin/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
var (
level zapcore.Level // zap 日志等级
options []zap.Option // zap 配置项
)
func InitializeLog() *zap.Logger {
// 创建根目录
createRootDir()
// 设置日志等级
setLogLevel()
if global.App.Config.Log.ShowLine {
options = append(options, zap.AddCaller())
}
// 初始化 zap
return zap.New(getZapCore(), options...)
}
func createRootDir() {
if ok, _ := utils.PathExists(global.App.Config.Log.RootDir); !ok {
_ = os.Mkdir(global.App.Config.Log.RootDir, os.ModePerm)
}
}
func setLogLevel() {
switch global.App.Config.Log.Level {
case "debug":
level = zap.DebugLevel
options = append(options, zap.AddStacktrace(level))
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
options = append(options, zap.AddStacktrace(level))
case "dpanic":
level = zap.DPanicLevel
case "panic":
level = zap.PanicLevel
case "fatal":
level = zap.FatalLevel
default:
level = zap.InfoLevel
}
}
// 扩展 Zap
func getZapCore() zapcore.Core {
var encoder zapcore.Encoder
// 调整编码器默认配置
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(time.Format("[" + "2006-01-02 15:04:05.000" + "]"))
}
encoderConfig.EncodeLevel = func(l zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(global.App.Config.App.Env + "." + l.String())
}
// 设置编码器
if global.App.Config.Log.Format == "json" {
encoder = zapcore.NewJSONEncoder(encoderConfig)
} else {
encoder = zapcore.NewConsoleEncoder(encoderConfig)
}
return zapcore.NewCore(encoder, getLogWriter(), level)
}
// 使用 lumberjack 作为日志写入器
func getLogWriter() zapcore.WriteSyncer {
file := &lumberjack.Logger{
Filename: global.App.Config.Log.RootDir + "/" + global.App.Config.Log.Filename,
MaxSize: global.App.Config.Log.MaxSize,
MaxBackups: global.App.Config.Log.MaxBackups,
MaxAge: global.App.Config.Log.MaxAge,
Compress: global.App.Config.Log.Compress,
}
return zapcore.AddSync(file)
}