细分了日志等级
parent
33621e61e1
commit
68a95cf351
|
@ -9,19 +9,40 @@ type LogFileConfig struct {
|
||||||
|
|
||||||
type ZapConfig struct {
|
type ZapConfig struct {
|
||||||
JsonFormat bool // 是否使用json格式,默认false
|
JsonFormat bool // 是否使用json格式,默认false
|
||||||
NormalLog LogFileConfig // 普通日志配置
|
DebugLog LogFileConfig // Debug日志配置
|
||||||
ErrorLog LogFileConfig // 错误日志配置
|
InfoLog LogFileConfig // Info日志配置
|
||||||
|
WarnLog LogFileConfig // Warn日志配置
|
||||||
|
ErrorLog LogFileConfig // Error日志配置
|
||||||
|
FatalLog LogFileConfig // Fatal日志配置
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg_default = ZapConfig{
|
var cfg_default = ZapConfig{
|
||||||
NormalLog: LogFileConfig{
|
DebugLog: LogFileConfig{
|
||||||
Filename: "./log/normal_log",
|
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,
|
FileSize_MB: 5,
|
||||||
FileBackup: 5,
|
FileBackup: 5,
|
||||||
FileAge_DAY: 30,
|
FileAge_DAY: 30,
|
||||||
},
|
},
|
||||||
ErrorLog: LogFileConfig{
|
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,
|
FileSize_MB: 5,
|
||||||
FileBackup: 5,
|
FileBackup: 5,
|
||||||
FileAge_DAY: 30,
|
FileAge_DAY: 30,
|
||||||
|
@ -33,18 +54,46 @@ func configDefault(config ...ZapConfig) ZapConfig {
|
||||||
return cfg_default
|
return cfg_default
|
||||||
}
|
}
|
||||||
cfg := config[0]
|
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 {
|
if cfg.DebugLog.FileSize_MB == 0 {
|
||||||
cfg.NormalLog.FileSize_MB = cfg_default.NormalLog.FileSize_MB
|
cfg.DebugLog.FileSize_MB = cfg_default.DebugLog.FileSize_MB
|
||||||
}
|
}
|
||||||
if cfg.NormalLog.FileBackup == 0 {
|
if cfg.DebugLog.FileBackup == 0 {
|
||||||
cfg.NormalLog.FileBackup = cfg_default.NormalLog.FileBackup
|
cfg.DebugLog.FileBackup = cfg_default.DebugLog.FileBackup
|
||||||
}
|
}
|
||||||
if cfg.NormalLog.FileAge_DAY == 0 {
|
if cfg.DebugLog.FileAge_DAY == 0 {
|
||||||
cfg.NormalLog.FileAge_DAY = cfg_default.NormalLog.FileAge_DAY
|
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 == "" {
|
if cfg.ErrorLog.Filename == "" {
|
||||||
cfg.ErrorLog.Filename = cfg_default.ErrorLog.Filename
|
cfg.ErrorLog.Filename = cfg_default.ErrorLog.Filename
|
||||||
}
|
}
|
||||||
|
@ -57,5 +106,19 @@ func configDefault(config ...ZapConfig) ZapConfig {
|
||||||
if cfg.ErrorLog.FileAge_DAY == 0 {
|
if cfg.ErrorLog.FileAge_DAY == 0 {
|
||||||
cfg.ErrorLog.FileAge_DAY = cfg_default.ErrorLog.FileAge_DAY
|
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
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,11 @@ import (
|
||||||
|
|
||||||
func textencoderConfig() zapcore.EncoderConfig {
|
func textencoderConfig() zapcore.EncoderConfig {
|
||||||
encoderConfig := zap.NewProductionEncoderConfig()
|
encoderConfig := zap.NewProductionEncoderConfig()
|
||||||
//时间格式
|
// 时间格式
|
||||||
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")
|
enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")
|
||||||
}
|
}
|
||||||
//日志等级字母大写
|
// 日志等级字母大写
|
||||||
encoderConfig.EncodeLevel = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
encoderConfig.EncodeLevel = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
enc.AppendString("[" + level.CapitalString() + "]")
|
enc.AppendString("[" + level.CapitalString() + "]")
|
||||||
}
|
}
|
||||||
|
@ -24,11 +24,11 @@ func textencoderConfig() zapcore.EncoderConfig {
|
||||||
|
|
||||||
func jsonencoderConfig() zapcore.EncoderConfig {
|
func jsonencoderConfig() zapcore.EncoderConfig {
|
||||||
encoderConfig := zap.NewProductionEncoderConfig()
|
encoderConfig := zap.NewProductionEncoderConfig()
|
||||||
//时间格式
|
// 时间格式
|
||||||
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
enc.AppendString(t.Format("2006-01-02 15:04:05"))
|
enc.AppendString(t.Format("2006-01-02 15:04:05"))
|
||||||
}
|
}
|
||||||
//日志等级字母大写
|
// 日志等级字母大写
|
||||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||||
return encoderConfig
|
return encoderConfig
|
||||||
}
|
}
|
||||||
|
@ -42,29 +42,66 @@ func New(config ...ZapConfig) *zap.SugaredLogger {
|
||||||
encoder = zapcore.NewJSONEncoder(jsonencoderConfig())
|
encoder = zapcore.NewJSONEncoder(jsonencoderConfig())
|
||||||
}
|
}
|
||||||
|
|
||||||
//日志级别 error 级别
|
var coreArr []zapcore.Core
|
||||||
highPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
|
||||||
return lev >= zap.ErrorLevel
|
// 日志级别 Debug 级别
|
||||||
|
DebugPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||||
|
return lev == zap.DebugLevel
|
||||||
})
|
})
|
||||||
//info 和 debug 级别,debug 级别是最低的
|
|
||||||
lowPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
// Debug 文件 writeSyncer
|
||||||
return lev < zap.ErrorLevel && lev >= zap.DebugLevel
|
DebugFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
||||||
})
|
Filename: cfg.DebugLog.Filename,
|
||||||
//info文件 writeSyncer
|
MaxSize: cfg.DebugLog.FileSize_MB,
|
||||||
infoFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
MaxBackups: cfg.DebugLog.FileBackup,
|
||||||
Filename: cfg.NormalLog.Filename,
|
MaxAge: cfg.DebugLog.FileAge_DAY,
|
||||||
MaxSize: cfg.NormalLog.FileSize_MB,
|
|
||||||
MaxBackups: cfg.NormalLog.FileBackup,
|
|
||||||
MaxAge: cfg.NormalLog.FileAge_DAY,
|
|
||||||
Compress: true,
|
Compress: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
//第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志
|
DebugFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(DebugFileWriteSyncer, zapcore.AddSync(os.Stdout)), DebugPriority)
|
||||||
infoFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(infoFileWriteSyncer,
|
coreArr = append(coreArr, DebugFileCore)
|
||||||
zapcore.AddSync(os.Stdout)), lowPriority)
|
|
||||||
|
|
||||||
//error文件writeSyncer
|
// 日志级别 Info 级别
|
||||||
errorFileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
|
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,
|
Filename: cfg.ErrorLog.Filename,
|
||||||
MaxSize: cfg.ErrorLog.FileSize_MB,
|
MaxSize: cfg.ErrorLog.FileSize_MB,
|
||||||
MaxBackups: cfg.ErrorLog.FileBackup,
|
MaxBackups: cfg.ErrorLog.FileBackup,
|
||||||
|
@ -72,13 +109,25 @@ func New(config ...ZapConfig) *zap.SugaredLogger {
|
||||||
Compress: true,
|
Compress: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
//第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志
|
ErrorFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(ErrorFileWriteSyncer, zapcore.AddSync(os.Stdout)), ErrorPriority)
|
||||||
errorFileCore := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(errorFileWriteSyncer,
|
coreArr = append(coreArr, ErrorFileCore)
|
||||||
zapcore.AddSync(os.Stdout)), highPriority)
|
|
||||||
var coreArr []zapcore.Core
|
// 日志级别 DPanic\Panic\Fatal 级别
|
||||||
coreArr = append(coreArr, infoFileCore)
|
HighPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||||
coreArr = append(coreArr, errorFileCore)
|
return lev >= zap.DPanicLevel
|
||||||
//zap.AddCaller()为显示文件名和行号,可省略
|
})
|
||||||
// return zap.New(zapcore.NewTee(coreArr...), zap.AddCaller())
|
|
||||||
|
// 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()
|
return zap.New(zapcore.NewTee(coreArr...)).Sugar()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue