From b5a07c6ba89dd7aa0903ba09fd3a5059a3318f3b Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 26 Jan 2016 09:20:49 +0800 Subject: [PATCH] log file name bug fixed this bug happens when daily rotate. ex,when it is 2016-01-22 23:59:59 and need a rotate,the file name should named with 2016-01-22 but named with 2016-01-23(next day) --- logs/file.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logs/file.go b/logs/file.go index f277317c..e7401922 100644 --- a/logs/file.go +++ b/logs/file.go @@ -127,7 +127,7 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { if w.needRotate(len(msg), d) { w.Lock() if w.needRotate(len(msg), d) { - if err := w.doRotate(); err != nil { + if err := w.doRotate(when); err != nil { fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) } @@ -200,7 +200,7 @@ func (w *fileLogWriter) lines() (int, error) { // DoRotate means it need to write file in new file. // new file name like xx.2013-01-01.2.log -func (w *fileLogWriter) doRotate() error { +func (w *fileLogWriter) doRotate(logTime time.Time) error { _, err := os.Lstat(w.Filename) if err != nil { return err @@ -215,7 +215,7 @@ func (w *fileLogWriter) doRotate() error { suffix = ".log" } for ; err == nil && num <= 999; num++ { - fName = filenameOnly + fmt.Sprintf(".%s.%03d%s", time.Now().Format("2006-01-02"), num, suffix) + fName = filenameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, suffix) _, err = os.Lstat(fName) } // return error if the last file checked still existed