80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package zaplog
|
|
|
|
const (
|
|
Debug int8 = iota - 1
|
|
Info
|
|
Warn
|
|
Error
|
|
DPanic
|
|
Panic
|
|
Fatal
|
|
)
|
|
|
|
type LogFileConfig struct {
|
|
Filename string // 日志文件路径及名称(自动创建不存在的目录)
|
|
FileSize_MB int // 文件大小限制(MB)
|
|
FileBackup int // 最大保留日志文件数
|
|
FileAge_DAY int // 日志文件保留天数
|
|
Compress bool // 是否压缩日志
|
|
}
|
|
|
|
type Config struct {
|
|
JsonFormat bool // 是否使用JSON格式(默认false)
|
|
Mode int8 // 日志启用级别(使用上面定义的常量)
|
|
DebugLog LogFileConfig // Debug日志配置
|
|
InfoLog LogFileConfig // Info日志配置
|
|
WarnLog LogFileConfig // Warn日志配置
|
|
ErrorLog LogFileConfig // Error日志配置
|
|
DPanicLog LogFileConfig // DPanic日志配置
|
|
PanicLog LogFileConfig // Panic日志配置
|
|
FatalLog LogFileConfig // Fatal日志配置
|
|
}
|
|
|
|
var cfg_default = Config{
|
|
DebugLog: LogFileConfig{
|
|
Filename: "./log/Info",
|
|
FileSize_MB: 5,
|
|
FileBackup: 5,
|
|
FileAge_DAY: 30,
|
|
Compress: true,
|
|
},
|
|
}
|
|
|
|
// 辅助函数:应用默认值
|
|
func applyDefaults(target, source *LogFileConfig) {
|
|
if target.Filename == "" {
|
|
target.Filename = source.Filename
|
|
}
|
|
if target.FileSize_MB == 0 {
|
|
target.FileSize_MB = source.FileSize_MB
|
|
}
|
|
if target.FileBackup == 0 {
|
|
target.FileBackup = source.FileBackup
|
|
}
|
|
if target.FileAge_DAY == 0 {
|
|
target.FileAge_DAY = source.FileAge_DAY
|
|
}
|
|
if !target.Compress && source.Compress {
|
|
target.Compress = source.Compress
|
|
}
|
|
}
|
|
|
|
func configDefault(config ...Config) Config {
|
|
if len(config) == 0 {
|
|
return cfg_default
|
|
}
|
|
|
|
cfg := config[0]
|
|
|
|
// 为每个日志级别应用默认值
|
|
applyDefaults(&cfg.DebugLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.InfoLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.WarnLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.ErrorLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.DPanicLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.PanicLog, &cfg_default.DebugLog)
|
|
applyDefaults(&cfg.FatalLog, &cfg_default.DebugLog)
|
|
|
|
return cfg
|
|
}
|