From 759c1bc282380f479c1101b3bf6dc5e3ab0f86d2 Mon Sep 17 00:00:00 2001 From: woshikedayaa Date: Fri, 22 Dec 2023 16:37:44 +0800 Subject: [PATCH] add a option to support custom time format --- .gitignore | 1 + lumberjack.go | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8365624..410cc5e 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ _testmain.go *.exe *.test +.idea \ No newline at end of file diff --git a/lumberjack.go b/lumberjack.go index 3447cdc..45e46e8 100644 --- a/lumberjack.go +++ b/lumberjack.go @@ -3,7 +3,7 @@ // Note that this is v2.0 of lumberjack, and should be imported using gopkg.in // thusly: // -// import "gopkg.in/natefinch/lumberjack.v2" +// import "gopkg.in/natefinch/lumberjack.v2" // // The package name remains simply lumberjack, and the code resides at // https://github.com/natefinch/lumberjack under the v2.0 branch. @@ -66,7 +66,7 @@ var _ io.WriteCloser = (*Logger)(nil) // `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would // use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log` // -// Cleaning Up Old Log Files +// # Cleaning Up Old Log Files // // Whenever a new logfile gets created, old log files may be deleted. The most // recent files according to the encoded timestamp will be retained, up to a @@ -107,6 +107,10 @@ type Logger struct { // using gzip. The default is not to perform compression. Compress bool `json:"compress" yaml:"compress"` + // BackupTimeFormat specifies the formatting standard for time when creating backup log files. + // The default is backupTimeFormat + BackupTimeFormat string `json:"backuptimeformat" yaml:"backuptimeformat"` + size int64 file *os.File mu sync.Mutex @@ -218,7 +222,10 @@ func (l *Logger) openNew() error { // Copy the mode off the old logfile. mode = info.Mode() // move the existing file - newname := backupName(name, l.LocalTime) + if len(l.BackupTimeFormat) == 0 { + l.BackupTimeFormat = backupTimeFormat + } + newname := backupName(name, l.LocalTime, l.BackupTimeFormat) if err := os.Rename(name, newname); err != nil { return fmt.Errorf("can't rename log file: %s", err) } @@ -244,7 +251,7 @@ func (l *Logger) openNew() error { // backupName creates a new filename from the given name, inserting a timestamp // between the filename and the extension, using the local time if requested // (otherwise UTC). -func backupName(name string, local bool) string { +func backupName(name string, local bool, timeformat string) string { dir := filepath.Dir(name) filename := filepath.Base(name) ext := filepath.Ext(filename) @@ -254,7 +261,7 @@ func backupName(name string, local bool) string { t = t.UTC() } - timestamp := t.Format(backupTimeFormat) + timestamp := t.Format(timeformat) return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext)) } @@ -438,7 +445,10 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) { return time.Time{}, errors.New("mismatched extension") } ts := filename[len(prefix) : len(filename)-len(ext)] - return time.Parse(backupTimeFormat, ts) + if len(l.BackupTimeFormat) == 0 { + l.BackupTimeFormat = backupTimeFormat + } + return time.Parse(l.BackupTimeFormat, ts) } // max returns the maximum size in bytes of log files before rolling.