Skip to content

Commit

Permalink
fix: fix the gen logfile error on set rotateTime is mintes. close #150
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 14, 2024
1 parent 1da33a3 commit 3c47826
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}
3 changes: 2 additions & 1 deletion rotatefile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions rotatefile/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 3c47826

Please sign in to comment.