From 07aa97aa9a4553dd2273d4d2bf65f8046adcdf49 Mon Sep 17 00:00:00 2001 From: chenxiaonan01 Date: Wed, 20 Dec 2017 15:56:36 +0800 Subject: [PATCH 1/6] add hourly rotate file.go --- logs/file.go | 92 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/logs/file.go b/logs/file.go index 8e5117d2..285cedae 100644 --- a/logs/file.go +++ b/logs/file.go @@ -50,6 +50,12 @@ type fileLogWriter struct { dailyOpenDate int dailyOpenTime time.Time + // Rotate hourly + Hourly bool `json:"hourly"` + MaxHours int64 `json:"maxhours"` + hourlyOpenDate int + hourlyOpenTime time.Time + Rotate bool `json:"rotate"` Level int `json:"level"` @@ -66,6 +72,8 @@ func newFileWriter() Logger { w := &fileLogWriter{ Daily: true, MaxDays: 7, + Hourly: false, + MaxHours: 168, Rotate: true, RotatePerm: "0440", Level: LevelTrace, @@ -76,15 +84,15 @@ func newFileWriter() Logger { // Init file logger with json config. // jsonConfig like: -// { -// "filename":"logs/beego.log", -// "maxLines":10000, -// "maxsize":1024, -// "daily":true, -// "maxDays":15, -// "rotate":true, -// "perm":"0600" -// } +// { +// "filename":"logs/beego.log", +// "maxLines":10000, +// "maxsize":1024, +// "daily":true, +// "maxDays":15, +// "rotate":true, +// "perm":"0600" +// } func (w *fileLogWriter) Init(jsonConfig string) error { err := json.Unmarshal([]byte(jsonConfig), w) if err != nil { @@ -119,6 +127,12 @@ func (w *fileLogWriter) needRotate(size int, day int) bool { return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) || (w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) || (w.Daily && day != w.dailyOpenDate) +} + +func (w *fileLogWriter) needRotateHourly(size int, hour int) bool { + return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) || + (w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) || + (w.Hourly && hour != w.hourlyOpenDate) } @@ -127,14 +141,23 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { if level > w.Level { return nil } - h, d := formatTimeHeader(when) - msg = string(h) + msg + "\n" + hd, d, h := formatTimeHeader(when) + msg = string(hd) + msg + "\n" if w.Rotate { w.RLock() - if w.needRotate(len(msg), d) { + if w.needRotateHourly(len(msg), h) { w.RUnlock() w.Lock() - if w.needRotate(len(msg), d) { + if w.needRotateHourly(len(msg), h) { + if err := w.doRotate(when); err != nil { + fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) + } + } + w.Unlock() + } else if w.needRotateDaily(len(msg), d) { + w.RUnlock() + w.Lock() + if w.needRotateDaily(len(msg), d) { if err := w.doRotate(when); err != nil { fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) } @@ -178,8 +201,12 @@ func (w *fileLogWriter) initFd() error { w.maxSizeCurSize = int(fInfo.Size()) w.dailyOpenTime = time.Now() w.dailyOpenDate = w.dailyOpenTime.Day() + w.hourlyOpenTime = time.Now() + w.hourlyOpenDate = w.hourlyOpenTime.Hour() w.maxLinesCurLines = 0 - if w.Daily { + if w.Hourly { + go w.hourlyRotate(w.hourlyOpenTime) + } else if w.Daily { go w.dailyRotate(w.dailyOpenTime) } if fInfo.Size() > 0 && w.MaxLines > 0 { @@ -198,7 +225,22 @@ func (w *fileLogWriter) dailyRotate(openTime time.Time) { tm := time.NewTimer(time.Duration(nextDay.UnixNano() - openTime.UnixNano() + 100)) <-tm.C w.Lock() - if w.needRotate(0, time.Now().Day()) { + if w.needRotateDaily(0, time.Now().Day()) { + if err := w.doRotate(time.Now()); err != nil { + fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) + } + } + w.Unlock() +} + +func (w *fileLogWriter) hourlyRotate(openTime time.Time) { + y, m, d := openTime.Add(1 * time.Hour).Date() + h, _, _ := openTime.Add(1 * time.Hour).Clock() + nextHour := time.Date(y, m, d, h, 0, 0, 0, openTime.Location()) + tm := time.NewTimer(time.Duration(nextHour.UnixNano() - openTime.UnixNano() + 100)) + <-tm.C + w.Lock() + if w.needRotateHourly(0, time.Now().Hour()) { if err := w.doRotate(time.Now()); err != nil { fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) } @@ -239,7 +281,10 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { // file exists // Find the next available number num := 1 + fmt.Println("num :", num) fName := "" + format := "" + var openTime time.Time rotatePerm, err := strconv.ParseInt(w.RotatePerm, 8, 64) if err != nil { return err @@ -251,19 +296,29 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { goto RESTART_LOGGER } + if w.Hourly { + format = "2006010215" + openTime = w.hourlyOpenTime + } else if w.Daily { + format = "2006-01-02" + openTime = w.dailyOpenTime + } + if w.MaxLines > 0 || w.MaxSize > 0 { for ; err == nil && num <= 999; num++ { - fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix) + fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format(format), num, w.suffix) _, err = os.Lstat(fName) } } else { - fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, w.dailyOpenTime.Format("2006-01-02"), w.suffix) + fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, openTime.Format(format), w.suffix) _, err = os.Lstat(fName) + fmt.Println("befor fName:", fName) for ; err == nil && num <= 999; num++ { - fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", w.dailyOpenTime.Format("2006-01-02"), num, w.suffix) + fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix) _, err = os.Lstat(fName) } } + fmt.Println("after fName:", fName) // return error if the last file checked still existed if err == nil { return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename) @@ -314,6 +369,7 @@ func (w *fileLogWriter) deleteOldLog() { os.Remove(path) } } + os.Exit(0) return }) } From e81cca304b59679cc9aec9a37824b20d10226987 Mon Sep 17 00:00:00 2001 From: chenxiaonan01 Date: Wed, 20 Dec 2017 16:19:58 +0800 Subject: [PATCH 2/6] add file test --- logs/file.go | 2 +- logs/file_test.go | 113 ++++++++++++++++++++++++++++++++++++++------ logs/logger.go | 6 +-- logs/logger_test.go | 4 +- 4 files changed, 105 insertions(+), 20 deletions(-) diff --git a/logs/file.go b/logs/file.go index 285cedae..9c29e487 100644 --- a/logs/file.go +++ b/logs/file.go @@ -123,7 +123,7 @@ func (w *fileLogWriter) startLogger() error { return w.initFd() } -func (w *fileLogWriter) needRotate(size int, day int) bool { +func (w *fileLogWriter) needRotateDaily(size int, day int) bool { return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) || (w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) || (w.Daily && day != w.dailyOpenDate) diff --git a/logs/file_test.go b/logs/file_test.go index 626521b9..621638b7 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -112,7 +112,7 @@ func TestFile2(t *testing.T) { os.Remove("test2.log") } -func TestFileRotate_01(t *testing.T) { +func TestFileDailyRotate_01(t *testing.T) { log := NewLogger(10000) log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) log.Debug("debug") @@ -133,28 +133,28 @@ func TestFileRotate_01(t *testing.T) { os.Remove("test3.log") } -func TestFileRotate_02(t *testing.T) { +func TestFileDailyRotate_02(t *testing.T) { fn1 := "rotate_day.log" fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" - testFileRotate(t, fn1, fn2) + testFileRotate(t, fn1, fn2, true, false) } -func TestFileRotate_03(t *testing.T) { +func TestFileDailyRotate_03(t *testing.T) { fn1 := "rotate_day.log" fn := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" os.Create(fn) fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".001.log" - testFileRotate(t, fn1, fn2) + testFileRotate(t, fn1, fn2, true, false) os.Remove(fn) } -func TestFileRotate_04(t *testing.T) { +func TestFileDailyRotate_04(t *testing.T) { fn1 := "rotate_day.log" fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" testFileDailyRotate(t, fn1, fn2) } -func TestFileRotate_05(t *testing.T) { +func TestFileDailyRotate_05(t *testing.T) { fn1 := "rotate_day.log" fn := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" os.Create(fn) @@ -162,7 +162,7 @@ func TestFileRotate_05(t *testing.T) { testFileDailyRotate(t, fn1, fn2) os.Remove(fn) } -func TestFileRotate_06(t *testing.T) { //test file mode +func TestFileDailyRotate_06(t *testing.T) { //test file mode log := NewLogger(10000) log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) log.Debug("debug") @@ -183,19 +183,104 @@ func TestFileRotate_06(t *testing.T) { //test file mode os.Remove(rotateName) os.Remove("test3.log") } -func testFileRotate(t *testing.T, fn1, fn2 string) { + +func TestFileHourlyRotate_01(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + log.Debug("debug") + log.Info("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + rotateName := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006010215"), 1) + ".log" + b, err := exists(rotateName) + if !b || err != nil { + os.Remove("test3.log") + t.Fatal("rotate not generated") + } + os.Remove(rotateName) + os.Remove("test3.log") +} + +func TestFileHourlyRotate_02(t *testing.T) { + fn1 := "rotate_hour.log" + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + testFileRotate(t, fn1, fn2, false, true) +} + +func TestFileHourlyRotate_03(t *testing.T) { + fn1 := "rotate_hour.log" + fn := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + os.Create(fn) + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".001.log" + testFileRotate(t, fn1, fn2, false, true) + os.Remove(fn) +} + +/* +func TestFileHourlyRotate_04(t *testing.T) { + fn1 := "rotate_hour.log" + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + testFileHourlyRotate(t, fn1, fn2) +} +func TestFileHourlyRotate_05(t *testing.T) { + fn1 := "rotate_hour.log" + fn := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + os.Create(fn) + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".001.log" + testFileHourlyRotate(t, fn1, fn2) + os.Remove(fn) +} +func TestFileHourlyRotate_06(t *testing.T) { //test file mode + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + log.Debug("debug") + log.Info("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + rotateName := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006010215"), 1) + ".log" + s, _ := os.Lstat(rotateName) + if s.Mode() != 0440 { + os.Remove(rotateName) + os.Remove("test3.log") + t.Fatal("rotate file mode error") + } + os.Remove(rotateName) + os.Remove("test3.log") +} +*/ +func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) { fw := &fileLogWriter{ - Daily: true, + Daily: daily, MaxDays: 7, + Hourly: hourly, + MaxHours: 168, Rotate: true, Level: LevelTrace, Perm: "0660", RotatePerm: "0440", } - fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) - fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) - fw.dailyOpenDate = fw.dailyOpenTime.Day() - fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug) + + if daily { + fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) + fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) + fw.dailyOpenDate = fw.dailyOpenTime.Day() + } + + if hourly { + fw.Init(fmt.Sprintf(`{"filename":"%v","maxhours":1}`, fn1)) + fw.hourlyOpenTime = time.Now().Add(-1 * time.Hour) + fw.hourlyOpenDate = fw.hourlyOpenTime.Day() + } + + fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug) for _, file := range []string{fn1, fn2} { _, err := os.Stat(file) diff --git a/logs/logger.go b/logs/logger.go index 1700901f..fa6d9334 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -33,7 +33,7 @@ func newLogWriter(wr io.Writer) *logWriter { func (lg *logWriter) println(when time.Time, msg string) { lg.Lock() - h, _ := formatTimeHeader(when) + h, _, _:= formatTimeHeader(when) lg.writer.Write(append(append(h, msg...), '\n')) lg.Unlock() } @@ -90,7 +90,7 @@ const ( ns1 = `0123456789` ) -func formatTimeHeader(when time.Time) ([]byte, int) { +func formatTimeHeader(when time.Time) ([]byte, int, int) { y, mo, d := when.Date() h, mi, s := when.Clock() ns := when.Nanosecond()/1000000 @@ -123,7 +123,7 @@ func formatTimeHeader(when time.Time) ([]byte, int) { buf[23] = ' ' - return buf[0:], d + return buf[0:], d, h } var ( diff --git a/logs/logger_test.go b/logs/logger_test.go index 69a8f0d2..5a0a423d 100644 --- a/logs/logger_test.go +++ b/logs/logger_test.go @@ -30,7 +30,7 @@ func TestFormatHeader_0(t *testing.T) { if tm.Year() >= 2100 { break } - h, _ := formatTimeHeader(tm) + h, _, _ := formatTimeHeader(tm) if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { t.Log(tm) t.FailNow() @@ -48,7 +48,7 @@ func TestFormatHeader_1(t *testing.T) { if tm.Year() >= year+1 { break } - h, _ := formatTimeHeader(tm) + h, _, _ := formatTimeHeader(tm) if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { t.Log(tm) t.FailNow() From 1a42154c64f625037742e3fc68809653ee288837 Mon Sep 17 00:00:00 2001 From: chenxiaonan01 Date: Wed, 20 Dec 2017 17:54:40 +0800 Subject: [PATCH 3/6] add file test --- logs/file.go | 26 +++++++++++++++----------- logs/file_test.go | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/logs/file.go b/logs/file.go index 9c29e487..3e102f07 100644 --- a/logs/file.go +++ b/logs/file.go @@ -281,7 +281,6 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { // file exists // Find the next available number num := 1 - fmt.Println("num :", num) fName := "" format := "" var openTime time.Time @@ -312,13 +311,11 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { } else { fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, openTime.Format(format), w.suffix) _, err = os.Lstat(fName) - fmt.Println("befor fName:", fName) for ; err == nil && num <= 999; num++ { fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix) _, err = os.Lstat(fName) } } - fmt.Println("after fName:", fName) // return error if the last file checked still existed if err == nil { return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename) @@ -362,14 +359,21 @@ func (w *fileLogWriter) deleteOldLog() { if info == nil { return } - - if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) { - if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && - strings.HasSuffix(filepath.Base(path), w.suffix) { - os.Remove(path) - } - } - os.Exit(0) + if w.Hourly { + if !info.IsDir() && info.ModTime().Add(1*time.Hour*time.Duration(w.MaxHours)).Before(time.Now()) { + if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && + strings.HasSuffix(filepath.Base(path), w.suffix) { + os.Remove(path) + } + } + } else if w.Daily { + if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) { + if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && + strings.HasSuffix(filepath.Base(path), w.suffix) { + os.Remove(path) + } + } + } return }) } diff --git a/logs/file_test.go b/logs/file_test.go index 621638b7..333209c6 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -186,7 +186,7 @@ func TestFileDailyRotate_06(t *testing.T) { //test file mode func TestFileHourlyRotate_01(t *testing.T) { log := NewLogger(10000) - log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + log.SetLogger("file", `{"filename":"test3.log","hourly":true,"maxlines":4}`) log.Debug("debug") log.Info("info") log.Notice("notice") @@ -220,12 +220,12 @@ func TestFileHourlyRotate_03(t *testing.T) { os.Remove(fn) } -/* func TestFileHourlyRotate_04(t *testing.T) { fn1 := "rotate_hour.log" fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" testFileHourlyRotate(t, fn1, fn2) } + func TestFileHourlyRotate_05(t *testing.T) { fn1 := "rotate_hour.log" fn := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" @@ -234,9 +234,10 @@ func TestFileHourlyRotate_05(t *testing.T) { testFileHourlyRotate(t, fn1, fn2) os.Remove(fn) } + func TestFileHourlyRotate_06(t *testing.T) { //test file mode log := NewLogger(10000) - log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + log.SetLogger("file", `{"filename":"test3.log", "hourly":true, "maxlines":4}`) log.Debug("debug") log.Info("info") log.Notice("notice") @@ -255,7 +256,7 @@ func TestFileHourlyRotate_06(t *testing.T) { //test file mode os.Remove(rotateName) os.Remove("test3.log") } -*/ + func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) { fw := &fileLogWriter{ Daily: daily, @@ -324,6 +325,37 @@ func testFileDailyRotate(t *testing.T, fn1, fn2 string) { fw.Destroy() } +func testFileHourlyRotate(t *testing.T, fn1, fn2 string) { + fw := &fileLogWriter{ + Hourly: true, + MaxHours: 168, + Rotate: true, + Level: LevelTrace, + Perm: "0660", + RotatePerm: "0440", + } + fw.Init(fmt.Sprintf(`{"filename":"%v","maxhours":1}`, fn1)) + fw.hourlyOpenTime = time.Now().Add(-1 * time.Hour) + fw.hourlyOpenDate = fw.hourlyOpenTime.Hour() + hour, _ := time.ParseInLocation("2006010215", time.Now().Format("2006010215"), fw.hourlyOpenTime.Location()) + hour = hour.Add(-1 * time.Second) + fw.hourlyRotate(hour) + for _, file := range []string{fn1, fn2} { + _, err := os.Stat(file) + if err != nil { + t.FailNow() + } + content, err := ioutil.ReadFile(file) + if err != nil { + t.FailNow() + } + if len(content) > 0 { + t.FailNow() + } + os.Remove(file) + } + fw.Destroy() +} func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { From 31c746d9d758bec24c7559ea58541982a8aa4106 Mon Sep 17 00:00:00 2001 From: two Date: Thu, 26 Jul 2018 14:34:25 +0800 Subject: [PATCH 4/6] fix all confict --- logs/file.go | 36 ++++++------------------------------ logs/logger_test.go | 10 ---------- 2 files changed, 6 insertions(+), 40 deletions(-) diff --git a/logs/file.go b/logs/file.go index 21c4438e..588f7860 100644 --- a/logs/file.go +++ b/logs/file.go @@ -306,7 +306,6 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { goto RESTART_LOGGER } -<<<<<<< HEAD if w.Hourly { format = "2006010215" openTime = w.hourlyOpenTime @@ -315,31 +314,18 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error { openTime = w.dailyOpenTime } + // only when one of them be setted, then the file would be splited if w.MaxLines > 0 || w.MaxSize > 0 { - for ; err == nil && num <= 999; num++ { + for ; err == nil && num <= w.MaxFiles; num++ { fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format(format), num, w.suffix) _, err = os.Lstat(fName) } } else { - fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, openTime.Format(format), w.suffix) - _, err = os.Lstat(fName) - for ; err == nil && num <= 999; num++ { - fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix) - _, err = os.Lstat(fName) - } -======= - // only when one of them be setted, then the file would be splited - if w.MaxLines > 0 || w.MaxSize > 0 { - for ; err == nil && num <= w.MaxFiles; num++ { - fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix) - _, err = os.Lstat(fName) - } - } else { - fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", w.dailyOpenTime.Format("2006-01-02"), num, w.suffix) + fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix) _, err = os.Lstat(fName) w.MaxFilesCurFiles = num ->>>>>>> old/develop } + // return error if the last file checked still existed if err == nil { return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename) @@ -383,31 +369,21 @@ func (w *fileLogWriter) deleteOldLog() { if info == nil { return } -<<<<<<< HEAD if w.Hourly { - if !info.IsDir() && info.ModTime().Add(1*time.Hour*time.Duration(w.MaxHours)).Before(time.Now()) { + if !info.IsDir() && info.ModTime().Add(1 * time.Hour * time.Duration(w.MaxHours)).Before(time.Now()) { if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && strings.HasSuffix(filepath.Base(path), w.suffix) { os.Remove(path) } } } else if w.Daily { - if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) { + if !info.IsDir() && info.ModTime().Add(24 * time.Hour * time.Duration(w.MaxDays)).Before(time.Now()) { if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && strings.HasSuffix(filepath.Base(path), w.suffix) { os.Remove(path) } } } -======= - - if !info.IsDir() && info.ModTime().Add(24 * time.Hour * time.Duration(w.MaxDays)).Before(time.Now()) { - if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && - strings.HasSuffix(filepath.Base(path), w.suffix) { - os.Remove(path) - } - } ->>>>>>> old/develop return }) } diff --git a/logs/logger_test.go b/logs/logger_test.go index 870b0d81..78c67737 100644 --- a/logs/logger_test.go +++ b/logs/logger_test.go @@ -30,13 +30,8 @@ func TestFormatHeader_0(t *testing.T) { if tm.Year() >= 2100 { break } -<<<<<<< HEAD h, _, _ := formatTimeHeader(tm) - if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { -======= - h, _ := formatTimeHeader(tm) if tm.Format("2006/01/02 15:04:05.000 ") != string(h) { ->>>>>>> old/develop t.Log(tm) t.FailNow() } @@ -53,13 +48,8 @@ func TestFormatHeader_1(t *testing.T) { if tm.Year() >= year+1 { break } -<<<<<<< HEAD h, _, _ := formatTimeHeader(tm) - if tm.Format("2006/01/02 15:04:05.999 ") != string(h) { -======= - h, _ := formatTimeHeader(tm) if tm.Format("2006/01/02 15:04:05.000 ") != string(h) { ->>>>>>> old/develop t.Log(tm) t.FailNow() } From 046cb248e0d874818cba59f49c4106e4b551d30f Mon Sep 17 00:00:00 2001 From: two Date: Thu, 26 Jul 2018 15:08:14 +0800 Subject: [PATCH 5/6] edit test case --- logs/file_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logs/file_test.go b/logs/file_test.go index ad377f6d..e7c2ca9a 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -207,7 +207,7 @@ func TestFileHourlyRotate_01(t *testing.T) { func TestFileHourlyRotate_02(t *testing.T) { fn1 := "rotate_hour.log" - fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".001.log" testFileRotate(t, fn1, fn2, false, true) } @@ -222,7 +222,7 @@ func TestFileHourlyRotate_03(t *testing.T) { func TestFileHourlyRotate_04(t *testing.T) { fn1 := "rotate_hour.log" - fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".log" + fn2 := "rotate_hour." + time.Now().Add(-1*time.Hour).Format("2006010215") + ".001.log" testFileHourlyRotate(t, fn1, fn2) } From 39fc30b8b2268edbbdab2893b843ec6be50330aa Mon Sep 17 00:00:00 2001 From: guomao545 Date: Fri, 27 Jul 2018 15:33:24 +0800 Subject: [PATCH 6/6] Support return middle level value fix multilevel yaml config can't correct return middle level value bug --- config/yaml/yaml.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index 881737e3..7bf1335c 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -290,12 +290,15 @@ func (c *ConfigContainer) getData(key string) (interface{}, error) { keys := strings.Split(key, ".") tmpData := c.data - for _, k := range keys { + for idx, k := range keys { if v, ok := tmpData[k]; ok { switch v.(type) { case map[string]interface{}: { tmpData = v.(map[string]interface{}) + if idx == len(keys) - 1 { + return tmpData, nil + } } default: {