1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-06 03:53:50 +08:00

feat: log aging & force new log file. close #963

This commit is contained in:
Mrs4s 2021-07-17 22:58:45 +08:00
parent b68bb0762f
commit 85df77f9a5
No known key found for this signature in database
GPG Key ID: 3186E98FA19CE3A7
3 changed files with 22 additions and 4 deletions

View File

@ -57,6 +57,8 @@ type Config struct {
Output struct { Output struct {
LogLevel string `yaml:"log-level"` LogLevel string `yaml:"log-level"`
LogAging int `yaml:"log-aging"`
LogForceNew bool `yaml:"log-force-new"`
Debug bool `yaml:"debug"` Debug bool `yaml:"debug"`
} `yaml:"output"` } `yaml:"output"`

View File

@ -43,6 +43,10 @@ message:
output: output:
# 日志等级 trace,debug,info,warn,error # 日志等级 trace,debug,info,warn,error
log-level: warn log-level: warn
# 日志时效 单位天. 超过这个时间之前的日志将会被自动删除. 设置为 0 表示永久保留.
log-aging: 15
# 是否在每次启动时强制创建全新的文件储存日志. 为 false 的情况下将会在上次启动时创建的日志文件续写
log-force-new: true
# 是否启用 DEBUG # 是否启用 DEBUG
debug: false # 开启调试模式 debug: false # 开启调试模式

16
main.go
View File

@ -75,13 +75,25 @@ func init() {
LogFormat: "[%time%] [%lvl%]: %msg% \n", LogFormat: "[%time%] [%lvl%]: %msg% \n",
} }
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24)) conf = config.Get()
rotateOptions := []rotatelogs.Option{
rotatelogs.WithRotationTime(time.Hour * 24),
}
if conf.Output.LogAging > 0 {
rotateOptions = append(rotateOptions, rotatelogs.WithMaxAge(time.Hour*24*time.Duration(conf.Output.LogAging)))
}
if conf.Output.LogForceNew {
rotateOptions = append(rotateOptions, rotatelogs.ForceNewFile())
}
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotateOptions...)
if err != nil { if err != nil {
log.Errorf("rotatelogs init err: %v", err) log.Errorf("rotatelogs init err: %v", err)
panic(err) panic(err)
} }
conf = config.Get()
if debug { if debug {
conf.Output.Debug = true conf.Output.Debug = true
} }