From 3c478262ca1ad9dfefdc40a647a5ab877698c973 Mon Sep 17 00:00:00 2001 From: inhere Date: Sat, 14 Sep 2024 19:06:02 +0800 Subject: [PATCH] fix: fix the gen logfile error on set rotateTime is mintes. close #150 --- .github/workflows/go.yml | 2 +- issues_test.go | 24 +++++++++++++++++++++++ rotatefile/config.go | 3 ++- rotatefile/issues_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2d4b841..7c1591d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -18,7 +18,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go_version: [1.21, 1.19, '1.20'] + go_version: [1.22, 1.21, 1.19, '1.20'] os: [ubuntu-latest, windows-latest] # , macOS-latest steps: diff --git a/issues_test.go b/issues_test.go index 4bb4c0d..3317520 100644 --- a/issues_test.go +++ b/issues_test.go @@ -206,3 +206,27 @@ func TestIssues_137(t *testing.T) { assert.StrContains(t, content, "this is a log file content") assert.StrContains(t, content, "log index=4") } + +// https://github.com/gookit/slog/issues/144 +// slog: failed to handle log, error: write ./logs/info.log: file already closed #144 +func TestIssues_144(t *testing.T) { + defer slog.MustClose() + slog.Reset() + + // DangerLevels 包含: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel + h1 := handler.MustRotateFile("./testdata/logs/error_is144.log", rotatefile.EveryDay, + handler.WithLogLevels(slog.DangerLevels), + handler.WithCompress(true), + ) + + // NormalLevels 包含: slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel + h2 := handler.MustFileHandler("./testdata/logs/info_is144.log", handler.WithLogLevels(slog.NormalLevels)) + + // 注册 handler 到 logger(调度器) + slog.PushHandlers(h1, h2) + + // add logs + slog.Info("info message text") + slog.Error("error message text") + +} diff --git a/rotatefile/config.go b/rotatefile/config.go index cc19508..cd712b4 100644 --- a/rotatefile/config.go +++ b/rotatefile/config.go @@ -31,6 +31,7 @@ type RotateTime int // built in rotate time constants const ( + EveryMonth RotateTime = 30 * timex.OneDaySec EveryDay RotateTime = timex.OneDaySec EveryHour RotateTime = timex.OneHourSec Every30Min RotateTime = 30 * timex.OneMinSec @@ -67,7 +68,7 @@ func (rt RotateTime) FirstCheckTime(now time.Time) time.Time { // eg: now.Minute()=37, nextMin=42, will get nextDur=40 nextDur := time.Duration(nextMin).Round(time.Duration(minutes)) - return timex.HourStart(now).Add(nextDur) + return timex.HourStart(now).Add(nextDur * time.Minute) default: // levelSec return now.Add(time.Duration(interval) * time.Second) } diff --git a/rotatefile/issues_test.go b/rotatefile/issues_test.go index 4e2fac0..f278bef 100644 --- a/rotatefile/issues_test.go +++ b/rotatefile/issues_test.go @@ -49,3 +49,43 @@ func TestIssues_138(t *testing.T) { s = fsutil.ReadString(oldFile) assert.StrContains(t, s, "2023-11-16 23:") } + +// https://github.com/gookit/slog/issues/150 +// 日志轮转时间设置为分钟时,FirstCheckTime计算单位错误,导致生成预期外的多个日志文件 #150 +func TestIssues_150(t *testing.T) { + logfile := "testdata/i150_rotate_min.log" + + mt := rotatefile.NewMockClock("2024-09-14 18:39:55") + w, err := rotatefile.NewWriterWith(rotatefile.WithDebugMode, func(c *rotatefile.Config) { + c.TimeClock = mt + // c.MaxSize = 128 + c.Filepath = logfile + c.RotateTime = rotatefile.EveryMinute * 3 + }) + + assert.NoErr(t, err) + defer w.MustClose() + + for i := 0; i < 15; i++ { + dt := mt.Datetime() + _, err = w.WriteString(dt + " [INFO] this is a log message, idx=" + mathutil.String(i) + "\n") + assert.NoErr(t, err) + // increase time + mt.Add(time.Minute * 1) + } + + // Out: rotate_day.log, rotate_day.log.20231116 + files := fsutil.Glob(logfile + "*") + assert.LenGt(t, files, 3) + + // check contents + assert.True(t, fsutil.IsFile(logfile)) + s := fsutil.ReadString(logfile) + assert.StrContains(t, s, "2024-09-14 18:") + + // i150_rotate_min.log.20240914_1842 + oldFile := logfile + ".20240914_1842" + assert.True(t, fsutil.IsFile(oldFile)) + s = fsutil.ReadString(oldFile) + assert.StrContains(t, s, "2024-09-14 18:41") +}