允许不同级别的写入info

This commit is contained in:
yun
2026-10-05 19:30:40 +08:00
parent 7e1b474823
commit a9adc11fad
6 changed files with 600 additions and 32 deletions
+39 -25
View File
@@ -8,28 +8,28 @@ import (
)
type loggerOption struct {
prefix string // 日志前缀
format Format // json / text
dir string // 文件目录
isGinLog bool
isGid bool
isPrintFile bool
asGlobalLog bool // 是否接管全局 stdlib log(默认 false)
writeType writeType // 是否异步罗盘
traceField string // trace字段
errorToInfo bool // 错误日志是否写入info日志
days int // 日志保存天数
drivers []io.Writer // 文件落盘驱动器
fileSplit FileSplit // 文件切割规则
sizeSplit int // 根据文件大小切割(字节,<=0 不切割)
compress bool // 归档文件是否压缩为 .gz
compressLvl int // gzip 压缩级别
flushEvery time.Duration // 定时刷盘间隔,0 表示不开启
minLevel Level // 最低输出级别
onError func(error) // 内部错误回调
timeZone *time.Location // 时区
escapeHTML bool
expandData map[string]string // 扩展字段
prefix string // 日志前缀
format Format // json / text
dir string // 文件目录
isGinLog bool
isGid bool
isPrintFile bool
asGlobalLog bool // 是否接管全局 stdlib log(默认 false)
writeType writeType // 是否异步罗盘
traceField string // trace字段
allLevelToInfo bool // 非 info 级别的日志是否同时写入 info 日志
days int // 日志保存天数
drivers []io.Writer // 文件落盘驱动器
fileSplit FileSplit // 文件切割规则
sizeSplit int // 根据文件大小切割(字节,<=0 不切割)
compress bool // 归档文件是否压缩为 .gz
compressLvl int // gzip 压缩级别
flushEvery time.Duration // 定时刷盘间隔,0 表示不开启
minLevel Level // 最低输出级别
onError func(error) // 内部错误回调
timeZone *time.Location // 时区
escapeHTML bool
expandData map[string]string // 扩展字段
}
type writeType uint8
@@ -95,10 +95,24 @@ func SetToConsole() Option {
}
}
// 错误日志是否写入info日志
func SetErrorToInfo() Option {
// SetAllLevelToInfo 开启后,除 info 以外的所有级别(debug / warn / error)
// 都会额外往同一个 channel 的 info 日志里再写一份,
// info.log 因此成为「一个文件看全量日志」的汇总文件。
//
// 具体行为:
// - 各原级别文件照旧写(warn 进 warn.log、error 进 error.log),info.log 是副本;
// - 副本保留原始 level 字段,info.log 里仍能按级别区分和过滤;
// - info 本身不重复写,不会在 info.log 里出现两行同样的 info;
// - 副本只落文件,不会再走一遍额外驱动(控制台/自定义 writer),避免重复输出;
// - 级别过滤(SetMinLevel)仍然先生效:被丢弃的日志不会因为它而出现在 info.log 里;
// - SetPrintFile(false) 时该选项无意义(没有文件可写)。
//
// 代价:开启后 info.log 的体积约等于所有级别日志之和(保留期内磁盘占用同样成倍
// 增长);异步模式下每条非 info 日志会多占一个队列槽位。
// 按大小/时间切割与过期清理仍然照常生效。
func SetAllLevelToInfo() Option {
return func(o *loggerOption) {
o.errorToInfo = true
o.allLevelToInfo = true
}
}