package file import ( "io" "os" "path/filepath" "time" rotatelogs "github.com/lestrrat/go-file-rotatelogs" ) // 参考 https://github.com/nacos-group/nacos-sdk-go/blob/v1.0.4/common/logger/logger.go#L130 type RotateWriterConfig struct { Pattern string `json:"pattern"` FileName string `json:"file_path"` MaxAge time.Duration `json:"max_age"` RotateDuration time.Duration `json:"rotate_time"` } func NewDefaultRotateWriter(fileName string) RotateWriterConfig { return RotateWriterConfig{ FileName: fileName, MaxAge: time.Hour * 24 * 30, RotateDuration: time.Hour * 24, Pattern: "%Y%m%d%H%M", } } func (config RotateWriterConfig) NewWriter() (io.Writer, error) { fileSimpleName := filepath.Base(config.FileName) outputPath := filepath.Dir(config.FileName) + string(os.PathSeparator) return rotatelogs.New(filepath.Join(outputPath, fileSimpleName+"-"+config.Pattern), rotatelogs.WithRotationTime(config.RotateDuration), rotatelogs.WithMaxAge(config.MaxAge), rotatelogs.WithLinkName(filepath.Join(outputPath, fileSimpleName))) }