:::info
支持。
:::
使用 WithFileRotationP 即可
accessLogger, err := logger.NewJSONLogger( logger.WithDisableConsole(), logger.WithField("domain", fmt.Sprintf("%s[%s]", configs.ProjectName, env.Active().Value())), logger.WithTimeLayout(time_parse.CSTLayout), logger.WithFileP(configs.ProjectAccessLogFile),)// 这是初始化 Logger 的地方,当前使用的是 WithFileP 方法,还有一个 WithFileRotationP 方法。// WithFileRotationP 是支持日志分割的。---accessLogger, err := logger.NewJSONLogger( logger.WithDisableConsole(), logger.WithField("domain", fmt.Sprintf("%s[%s]", configs.ProjectName, env.Active().Value())), logger.WithTimeLayout(time_parse.CSTLayout), logger.WithFileRotationP(configs.ProjectAccessLogFile),)// 当使用 WithFileRotationP 时,这时日志就支持自动切割。
WithFileP 和 WithFileRotationP 代码片段
// see: pkg/logger/logger.go// WithFileP write log to some filefunc WithFileP(file string) Option { dir := filepath.Dir(file) if err := os.MkdirAll(dir, 0766); err != nil { panic(err) } f, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0766) if err != nil { panic(err) } return func(opt *option) { opt.file = zapcore.Lock(f) }}// WithFileRotationP write log to some file with rotationfunc WithFileRotationP(file string) Option { dir := filepath.Dir(file) if err := os.MkdirAll(dir, 0766); err != nil { panic(err) } return func(opt *option) { opt.file = &lumberjack.Logger{ // concurrent-safed Filename: file, // 文件路径 MaxSize: 128, // 单个文件最大尺寸,默认单位 M MaxBackups: 300, // 最多保留 300 个备份 MaxAge: 30, // 最大时间,默认单位 day LocalTime: true, // 使用本地时间 Compress: true, // 是否压缩 disabled by default } }}