diff --git a/global/config/config.go b/global/config/config.go index 62fcc40..11ca13b 100644 --- a/global/config/config.go +++ b/global/config/config.go @@ -56,8 +56,10 @@ type Config struct { } `yaml:"message"` Output struct { - LogLevel string `yaml:"log-level"` - Debug bool `yaml:"debug"` + LogLevel string `yaml:"log-level"` + LogAging int `yaml:"log-aging"` + LogForceNew bool `yaml:"log-force-new"` + Debug bool `yaml:"debug"` } `yaml:"output"` Servers []map[string]yaml.Node `yaml:"servers"` diff --git a/global/config/default_config.yml b/global/config/default_config.yml index d1cea09..802c271 100644 --- a/global/config/default_config.yml +++ b/global/config/default_config.yml @@ -43,6 +43,10 @@ message: output: # 日志等级 trace,debug,info,warn,error log-level: warn + # 日志时效 单位天. 超过这个时间之前的日志将会被自动删除. 设置为 0 表示永久保留. + log-aging: 15 + # 是否在每次启动时强制创建全新的文件储存日志. 为 false 的情况下将会在上次启动时创建的日志文件续写 + log-force-new: true # 是否启用 DEBUG debug: false # 开启调试模式 diff --git a/main.go b/main.go index 4eea4d2..dbacb8c 100644 --- a/main.go +++ b/main.go @@ -75,13 +75,25 @@ func init() { 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 { log.Errorf("rotatelogs init err: %v", err) panic(err) } - conf = config.Get() if debug { conf.Output.Debug = true }