diff --git a/logger.go b/logger.go index 8c5a8b7..a222247 100644 --- a/logger.go +++ b/logger.go @@ -55,6 +55,7 @@ type LoggerConfig struct { Format string `json:"format"` EncodeLevel zapcore.LevelEncoder `json:"encodeLevel"` Prefix string `json:"prefix"` + MaxAge uint `json:"maxAge"` } // InitLogger @Title 初始化日志工具 @@ -138,11 +139,16 @@ func (l *logger) pathExists(path string) (bool, error) { } func (l *logger) getWriteSyncer() (zapcore.WriteSyncer, error) { + options := []zaprotatelogs.Option{ + zaprotatelogs.WithLinkName(l.Config.LinkName), + zaprotatelogs.WithRotationTime(time.Hour * 24), + } + if l.Config.MaxAge > 0 { + options = append(options, zaprotatelogs.WithMaxAge(time.Hour*24*time.Duration(l.Config.MaxAge))) + } fileWriter, err := zaprotatelogs.New( path.Join(l.Config.Director, "%Y-%m-%d.log"), - zaprotatelogs.WithLinkName(l.Config.LinkName), - zaprotatelogs.WithMaxAge(time.Hour*24*7), - zaprotatelogs.WithRotationTime(time.Hour*24), + options..., ) if l.Config.LogInConsole { return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err