Skip to content

Commit 3c47826

Browse files
committed
fix: fix the gen logfile error on set rotateTime is mintes. close #150
1 parent 1da33a3 commit 3c47826

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ${{ matrix.os }}
1919
strategy:
2020
matrix:
21-
go_version: [1.21, 1.19, '1.20']
21+
go_version: [1.22, 1.21, 1.19, '1.20']
2222
os: [ubuntu-latest, windows-latest] # , macOS-latest
2323

2424
steps:

issues_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,27 @@ func TestIssues_137(t *testing.T) {
206206
assert.StrContains(t, content, "this is a log file content")
207207
assert.StrContains(t, content, "log index=4")
208208
}
209+
210+
// https://github.com/gookit/slog/issues/144
211+
// slog: failed to handle log, error: write ./logs/info.log: file already closed #144
212+
func TestIssues_144(t *testing.T) {
213+
defer slog.MustClose()
214+
slog.Reset()
215+
216+
// DangerLevels 包含: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel
217+
h1 := handler.MustRotateFile("./testdata/logs/error_is144.log", rotatefile.EveryDay,
218+
handler.WithLogLevels(slog.DangerLevels),
219+
handler.WithCompress(true),
220+
)
221+
222+
// NormalLevels 包含: slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel
223+
h2 := handler.MustFileHandler("./testdata/logs/info_is144.log", handler.WithLogLevels(slog.NormalLevels))
224+
225+
// 注册 handler 到 logger(调度器)
226+
slog.PushHandlers(h1, h2)
227+
228+
// add logs
229+
slog.Info("info message text")
230+
slog.Error("error message text")
231+
232+
}

rotatefile/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type RotateTime int
3131

3232
// built in rotate time constants
3333
const (
34+
EveryMonth RotateTime = 30 * timex.OneDaySec
3435
EveryDay RotateTime = timex.OneDaySec
3536
EveryHour RotateTime = timex.OneHourSec
3637
Every30Min RotateTime = 30 * timex.OneMinSec
@@ -67,7 +68,7 @@ func (rt RotateTime) FirstCheckTime(now time.Time) time.Time {
6768

6869
// eg: now.Minute()=37, nextMin=42, will get nextDur=40
6970
nextDur := time.Duration(nextMin).Round(time.Duration(minutes))
70-
return timex.HourStart(now).Add(nextDur)
71+
return timex.HourStart(now).Add(nextDur * time.Minute)
7172
default: // levelSec
7273
return now.Add(time.Duration(interval) * time.Second)
7374
}

rotatefile/issues_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,43 @@ func TestIssues_138(t *testing.T) {
4949
s = fsutil.ReadString(oldFile)
5050
assert.StrContains(t, s, "2023-11-16 23:")
5151
}
52+
53+
// https://github.com/gookit/slog/issues/150
54+
// 日志轮转时间设置为分钟时,FirstCheckTime计算单位错误,导致生成预期外的多个日志文件 #150
55+
func TestIssues_150(t *testing.T) {
56+
logfile := "testdata/i150_rotate_min.log"
57+
58+
mt := rotatefile.NewMockClock("2024-09-14 18:39:55")
59+
w, err := rotatefile.NewWriterWith(rotatefile.WithDebugMode, func(c *rotatefile.Config) {
60+
c.TimeClock = mt
61+
// c.MaxSize = 128
62+
c.Filepath = logfile
63+
c.RotateTime = rotatefile.EveryMinute * 3
64+
})
65+
66+
assert.NoErr(t, err)
67+
defer w.MustClose()
68+
69+
for i := 0; i < 15; i++ {
70+
dt := mt.Datetime()
71+
_, err = w.WriteString(dt + " [INFO] this is a log message, idx=" + mathutil.String(i) + "\n")
72+
assert.NoErr(t, err)
73+
// increase time
74+
mt.Add(time.Minute * 1)
75+
}
76+
77+
// Out: rotate_day.log, rotate_day.log.20231116
78+
files := fsutil.Glob(logfile + "*")
79+
assert.LenGt(t, files, 3)
80+
81+
// check contents
82+
assert.True(t, fsutil.IsFile(logfile))
83+
s := fsutil.ReadString(logfile)
84+
assert.StrContains(t, s, "2024-09-14 18:")
85+
86+
// i150_rotate_min.log.20240914_1842
87+
oldFile := logfile + ".20240914_1842"
88+
assert.True(t, fsutil.IsFile(oldFile))
89+
s = fsutil.ReadString(oldFile)
90+
assert.StrContains(t, s, "2024-09-14 18:41")
91+
}

0 commit comments

Comments
 (0)