diff --git a/core/logx/config.go b/core/logx/config.go index be09ae3a9116..40ddd2ac041a 100644 --- a/core/logx/config.go +++ b/core/logx/config.go @@ -10,4 +10,5 @@ type LogConf struct { Compress bool `json:",optional"` KeepDays int `json:",optional"` StackCooldownMillis int `json:",default=100"` + Size int64 `json:",optional"` } diff --git a/core/logx/logs.go b/core/logx/logs.go index 9e5c759d04e4..6eb001ccd84e 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -89,6 +89,7 @@ type ( gzipEnabled bool logStackCooldownMills int keepDays int + Size int64 } // LogOption defines the method to customize the logging. @@ -296,6 +297,13 @@ func WithKeepDays(days int) LogOption { } } +//WithSize customizes logging to size logs file size limit. +func WithSize(size int64) LogOption { + return func(options *logOptions) { + options.Size = size + } +} + // WithGzip customizes logging to automatically gzip the log files. func WithGzip() LogOption { return func(opts *logOptions) { @@ -308,7 +316,7 @@ func createOutput(path string) (io.WriteCloser, error) { return nil, ErrLogPathNotSet } - return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays, + return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays, options.Size, options.gzipEnabled), options.gzipEnabled) } @@ -433,7 +441,9 @@ func setupWithFiles(c LogConf) error { if c.KeepDays > 0 { opts = append(opts, WithKeepDays(c.KeepDays)) } - + if c.Size > 0 { + opts = append(opts, WithSize(c.Size)) + } accessFile := path.Join(c.Path, accessFilename) errorFile := path.Join(c.Path, errorFilename) severeFile := path.Join(c.Path, severeFilename) diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go index 3219ca967471..6455233644cc 100644 --- a/core/logx/rotatelogger.go +++ b/core/logx/rotatelogger.go @@ -60,23 +60,26 @@ type ( delimiter string days int gzip bool + size int64 } ) // DefaultRotateRule is a default log rotating rule, currently DailyRotateRule. -func DefaultRotateRule(filename, delimiter string, days int, gzip bool) RotateRule { +func DefaultRotateRule(filename, delimiter string, days int, size int64, gzip bool) RotateRule { return &DailyRotateRule{ rotatedTime: getNowDate(), filename: filename, delimiter: delimiter, days: days, gzip: gzip, + size: size, } } // BackupFileName returns the backup filename on rotating. func (r *DailyRotateRule) BackupFileName() string { - return fmt.Sprintf("%s%s%s", r.filename, r.delimiter, getNowDate()) + //return fmt.Sprintf("%s%s%s", r.filename, r.delimiter, getNowDate()) + return fmt.Sprintf("%s%s%s%s%s%s%s", r.filename, r.delimiter, getNowDate(), r.delimiter, getNowHour(), getNowMinute(), getNowSecond()) } // MarkRotated marks the rotated time of r to be the current time. @@ -105,7 +108,8 @@ func (r *DailyRotateRule) OutdatedFiles() []string { var buf strings.Builder boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat) - fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary) + //fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary) + fmt.Fprintf(&buf, "%s%s%s%s%s", r.filename, r.delimiter, boundary, r.delimiter, "000000") if r.gzip { buf.WriteString(".gz") } @@ -123,7 +127,15 @@ func (r *DailyRotateRule) OutdatedFiles() []string { // ShallRotate checks if the file should be rotated. func (r *DailyRotateRule) ShallRotate() bool { - return len(r.rotatedTime) > 0 && getNowDate() != r.rotatedTime + return (len(r.rotatedTime) > 0 && getNowDate() != r.rotatedTime) || (r.size > 0 && genFileSize(r.filename) >= r.size*1024*1024) +} + +func genFileSize(fName string) int64 { + fi, err := os.Stat(fName) + if err != nil || fi == nil { + return 0 + } + return fi.Size() } // NewLogger returns a RotateLogger with given filename and rule, etc. @@ -304,6 +316,30 @@ func getNowDate() string { return time.Now().Format(dateFormat) } +func getNowHour() (h string) { + hi, _, _ := time.Now().Clock() + if hi < 10 { + return fmt.Sprintf("0%d", hi) + } + return fmt.Sprintf("%d", hi) +} + +func getNowMinute() (m string) { + _, mi, _ := time.Now().Clock() + if mi < 10 { + return fmt.Sprintf("0%d", mi) + } + return fmt.Sprintf("%d", mi) +} + +func getNowSecond() (s string) { + _, _, si := time.Now().Clock() + if si < 10 { + return fmt.Sprintf("0%d", si) + } + return fmt.Sprintf("%d", si) +} + func gzipFile(file string) error { in, err := os.Open(file) if err != nil {