-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissues_test.go
More file actions
91 lines (75 loc) · 2.6 KB
/
Copy pathissues_test.go
File metadata and controls
91 lines (75 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rotatefile_test
import (
"testing"
"time"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/x/assert"
"github.com/gookit/rotatefile"
"github.com/gookit/rotatefile/internal"
)
// https://github.com/gookit/slog/issues/138
// 日志按everyday自动滚动,文件名的日期对应的是前一天的日志 #138
func TestIssues_138(t *testing.T) {
logfile := "testdata/iss138_rotate_day.log"
mt := rotatefile.NewMockClock("2023-11-16 23:59:55")
w, err := rotatefile.NewWriterWith(rotatefile.WithDebugMode, func(c *rotatefile.Config) {
c.TimeClock = mt
// c.MaxSize = 128
c.Filepath = logfile
c.RotateTime = rotatefile.EveryDay
})
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.Second * 3)
// mt.Add(time.Millisecond * 300)
}
// Out: rotate_day.log, rotate_day.log.20231116
files := fsutil.Glob(internal.BuildGlobPattern(logfile))
assert.Len(t, files, 2)
// check contents
assert.True(t, fsutil.IsFile(logfile))
s := fsutil.ReadString(logfile)
assert.StrContains(t, s, "2023-11-17 00:00")
oldFile := internal.AddSuffix2path(logfile, "20231116")
assert.True(t, fsutil.IsFile(oldFile))
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/iss150_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)
}
files := fsutil.Glob(internal.BuildGlobPattern(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:")
// iss150_rotate_min.20240914_1842.log
oldFile := internal.AddSuffix2path(logfile, "20240914_1842")
assert.True(t, fsutil.IsFile(oldFile))
s = fsutil.ReadString(oldFile)
assert.StrContains(t, s, "2024-09-14 18:41")
}