细分了日志等级
parent
33621e61e1
commit
68a95cf351
|
@ -9,19 +9,40 @@ type LogFileConfig struct {
|
|||
|
||||
type ZapConfig struct {
|
||||
JsonFormat bool // 是否使用json格式,默认false
|
||||
NormalLog LogFileConfig // 普通日志配置
|
||||
ErrorLog LogFileConfig // 错误日志配置
|
||||
DebugLog LogFileConfig // Debug日志配置
|
||||
InfoLog LogFileConfig // Info日志配置
|
||||
WarnLog LogFileConfig // Warn日志配置
|
||||
ErrorLog LogFileConfig // Error日志配置
|
||||
FatalLog LogFileConfig // Fatal日志配置
|
||||
}
|
||||
|
||||
var cfg_default = ZapConfig{
|
||||
NormalLog: LogFileConfig{
|
||||
Filename: "./log/normal_log",
|
||||
DebugLog: LogFileConfig{
|
||||
Filename: "./log/debug",
|
||||
FileSize_MB: 5,
|
||||
FileBackup: 5,
|
||||
FileAge_DAY: 30,
|
||||
},
|
||||
InfoLog: LogFileConfig{
|
||||
Filename: "./log/info",
|
||||
FileSize_MB: 5,
|
||||
FileBackup: 5,
|
||||
FileAge_DAY: 30,
|
||||
},
|
||||
WarnLog: LogFileConfig{
|
||||
Filename: "./log/warn",
|
||||
FileSize_MB: 5,
|
||||
FileBackup: 5,
|
||||
FileAge_DAY: 30,
|
||||
},
|
||||
ErrorLog: LogFileConfig{
|
||||
Filename: "./log/error_log",
|
||||
Filename: "./log/error",
|
||||
FileSize_MB: 5,
|
||||
FileBackup: 5,
|
||||
FileAge_DAY: 30,
|
||||
},
|
||||
FatalLog: LogFileConfig{
|
||||
Filename: "./log/fatal",
|
||||
FileSize_MB: 5,
|
||||
FileBackup: 5,
|
||||
FileAge_DAY: 30,
|
||||
|
@ -33,18 +54,46 @@ func configDefault(config ...ZapConfig) ZapConfig {
|
|||
return cfg_default
|
||||
}
|
||||
cfg := config[0]
|
||||
if cfg.NormalLog.Filename == "" {
|
||||
cfg.NormalLog.Filename = cfg_default.NormalLog.Filename
|
||||
|
||||
if cfg.DebugLog.Filename == "" {
|
||||
cfg.DebugLog.Filename = cfg_default.DebugLog.Filename
|
||||
}
|
||||
if cfg.NormalLog.FileSize_MB == 0 {
|
||||
cfg.NormalLog.FileSize_MB = cfg_default.NormalLog.FileSize_MB
|
||||
if cfg.DebugLog.FileSize_MB == 0 {
|
||||
cfg.DebugLog.FileSize_MB = cfg_default.DebugLog.FileSize_MB
|
||||
}
|
||||
if cfg.NormalLog.FileBackup == 0 {
|
||||
cfg.NormalLog.FileBackup = cfg_default.NormalLog.FileBackup
|
||||
if cfg.DebugLog.FileBackup == 0 {
|
||||
cfg.DebugLog.FileBackup = cfg_default.DebugLog.FileBackup
|
||||
}
|
||||
if cfg.NormalLog.FileAge_DAY == 0 {
|
||||
cfg.NormalLog.FileAge_DAY = cfg_default.NormalLog.FileAge_DAY
|
||||
if cfg.DebugLog.FileAge_DAY == 0 {
|
||||
cfg.DebugLog.FileAge_DAY = cfg_default.DebugLog.FileAge_DAY
|
||||
}
|
||||
|
||||
if cfg.InfoLog.Filename == "" {
|
||||
cfg.InfoLog.Filename = cfg_default.InfoLog.Filename
|
||||
}
|
||||
if cfg.InfoLog.FileSize_MB == 0 {
|
||||
cfg.InfoLog.FileSize_MB = cfg_default.InfoLog.FileSize_MB
|
||||
}
|
||||
if cfg.InfoLog.FileBackup == 0 {
|
||||
cfg.InfoLog.FileBackup = cfg_default.InfoLog.FileBackup
|
||||
}
|
||||
if cfg.InfoLog.FileAge_DAY == 0 {
|
||||
cfg.InfoLog.FileAge_DAY = cfg_default.InfoLog.FileAge_DAY
|
||||
}
|
||||
|
||||
if cfg.WarnLog.Filename == "" {
|
||||
cfg.WarnLog.Filename = cfg_default.WarnLog.Filename
|
||||
}
|
||||
if cfg.WarnLog.FileSize_MB == 0 {
|
||||
cfg.WarnLog.FileSize_MB = cfg_default.WarnLog.FileSize_MB
|
||||
}
|
||||
if cfg.WarnLog.FileBackup == 0 {
|
||||
cfg.WarnLog.FileBackup = cfg_default.WarnLog.FileBackup
|
||||
}
|
||||
if cfg.WarnLog.FileAge_DAY == 0 {
|
||||
cfg.WarnLog.FileAge_DAY = cfg_default.WarnLog.FileAge_DAY
|
||||
}
|
||||
|
||||
if cfg.ErrorLog.Filename == "" {
|
||||
cfg.ErrorLog.Filename = cfg_default.ErrorLog.Filename
|
||||
}
|
||||
|
@ -57,5 +106,19 @@ func configDefault(config ...ZapConfig) ZapConfig {
|
|||
if cfg.ErrorLog.FileAge_DAY == 0 {
|
||||
cfg.ErrorLog.FileAge_DAY = cfg_default.ErrorLog.FileAge_DAY
|
||||
}
|
||||
|
||||
if cfg.FatalLog.Filename == "" {
|
||||
cfg.FatalLog.Filename = cfg_default.FatalLog.Filename
|
||||
}
|
||||
if cfg.FatalLog.FileSize_MB == 0 {
|
||||
cfg.FatalLog.FileSize_MB = cfg_default.FatalLog.FileSize_MB
|
||||
}
|
||||
if cfg.FatalLog.FileBackup == 0 {
|
||||
cfg.FatalLog.FileBackup = cfg_default.FatalLog.FileBackup
|
||||
}
|
||||
if cfg.FatalLog.FileAge_DAY == 0 {
|
||||
cfg.FatalLog.FileAge_DAY = cfg_default.FatalLog.FileAge_DAY
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
|
|
@ -42,29 +42,66 @@ func New(config ...ZapConfig) *zap.SugaredLogger {
|
|||
encoder = zapcore.NewJSONEncoder(jsonencoderConfig())
|
||||
}
|
||||
|
||||
//日志级别 error 级别
|
||||
highPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev >= zap.ErrorLevel
|
||||
var coreArr []zapcore.Core
|
||||
|
||||
// 日志级别 Debug 级别
|
||||
DebugPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev == zap.DebugLevel
|
||||
})
|
||||
//info 和 debug 级别,debug 级别是最低的
|
||||
lowPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev < zap.ErrorLevel && lev >= zap.DebugLevel
|
||||
})
|
||||
//info文件 writeSyncer
|
||||
infoFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.NormalLog.Filename,
|
||||
MaxSize: cfg.NormalLog.FileSize_MB,
|
||||
MaxBackups: cfg.NormalLog.FileBackup,
|
||||
MaxAge: cfg.NormalLog.FileAge_DAY,
|
||||
|
||||
// Debug 文件 writeSyncer
|
||||
DebugFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.DebugLog.Filename,
|
||||
MaxSize: cfg.DebugLog.FileSize_MB,
|
||||
MaxBackups: cfg.DebugLog.FileBackup,
|
||||
MaxAge: cfg.DebugLog.FileAge_DAY,
|
||||
Compress: true,
|
||||
})
|
||||
|
||||
//第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志
|
||||
infoFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(infoFileWriteSyncer,
|
||||
zapcore.AddSync(os.Stdout)), lowPriority)
|
||||
DebugFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(DebugFileWriteSyncer, zapcore.AddSync(os.Stdout)), DebugPriority)
|
||||
coreArr = append(coreArr, DebugFileCore)
|
||||
|
||||
//error文件writeSyncer
|
||||
errorFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
// 日志级别 Info 级别
|
||||
InfoPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev == zap.InfoLevel
|
||||
})
|
||||
|
||||
// Info 文件 writeSyncer
|
||||
InfoFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.InfoLog.Filename,
|
||||
MaxSize: cfg.InfoLog.FileSize_MB,
|
||||
MaxBackups: cfg.InfoLog.FileBackup,
|
||||
MaxAge: cfg.InfoLog.FileAge_DAY,
|
||||
Compress: true,
|
||||
})
|
||||
|
||||
InfoFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(InfoFileWriteSyncer, zapcore.AddSync(os.Stdout)), InfoPriority)
|
||||
coreArr = append(coreArr, InfoFileCore)
|
||||
|
||||
// 日志级别 Warn 级别
|
||||
WarnPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev == zap.WarnLevel
|
||||
})
|
||||
|
||||
// Warn 文件 writeSyncer
|
||||
WarnFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.WarnLog.Filename,
|
||||
MaxSize: cfg.WarnLog.FileSize_MB,
|
||||
MaxBackups: cfg.WarnLog.FileBackup,
|
||||
MaxAge: cfg.WarnLog.FileAge_DAY,
|
||||
Compress: true,
|
||||
})
|
||||
|
||||
WarnFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(WarnFileWriteSyncer, zapcore.AddSync(os.Stdout)), WarnPriority)
|
||||
coreArr = append(coreArr, WarnFileCore)
|
||||
|
||||
// 日志级别 Error 级别
|
||||
ErrorPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev == zap.ErrorLevel
|
||||
})
|
||||
|
||||
// Error 文件 writeSyncer
|
||||
ErrorFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.ErrorLog.Filename,
|
||||
MaxSize: cfg.ErrorLog.FileSize_MB,
|
||||
MaxBackups: cfg.ErrorLog.FileBackup,
|
||||
|
@ -72,13 +109,25 @@ func New(config ...ZapConfig) *zap.SugaredLogger {
|
|||
Compress: true,
|
||||
})
|
||||
|
||||
//第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志
|
||||
errorFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(errorFileWriteSyncer,
|
||||
zapcore.AddSync(os.Stdout)), highPriority)
|
||||
var coreArr []zapcore.Core
|
||||
coreArr = append(coreArr, infoFileCore)
|
||||
coreArr = append(coreArr, errorFileCore)
|
||||
//zap.AddCaller()为显示文件名和行号,可省略
|
||||
// return zap.New(zapcore.NewTee(coreArr...), zap.AddCaller())
|
||||
ErrorFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(ErrorFileWriteSyncer, zapcore.AddSync(os.Stdout)), ErrorPriority)
|
||||
coreArr = append(coreArr, ErrorFileCore)
|
||||
|
||||
// 日志级别 DPanic\Panic\Fatal 级别
|
||||
HighPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev >= zap.DPanicLevel
|
||||
})
|
||||
|
||||
// High 文件 writeSyncer
|
||||
HighFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: cfg.FatalLog.Filename,
|
||||
MaxSize: cfg.FatalLog.FileSize_MB,
|
||||
MaxBackups: cfg.FatalLog.FileBackup,
|
||||
MaxAge: cfg.FatalLog.FileAge_DAY,
|
||||
Compress: true,
|
||||
})
|
||||
|
||||
HighFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(HighFileWriteSyncer, zapcore.AddSync(os.Stdout)), HighPriority)
|
||||
coreArr = append(coreArr, HighFileCore)
|
||||
|
||||
return zap.New(zapcore.NewTee(coreArr...)).Sugar()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue