-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrotatefile_test.go
180 lines (143 loc) · 4.59 KB
/
rotatefile_test.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package handler_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/timex"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"github.com/gookit/slog/rotatefile"
)
func TestNewRotateFileHandler(t *testing.T) {
// by size
logfile := "./testdata/both-rotate-bysize.log"
assert.NoErr(t, fsutil.DeleteIfFileExist(logfile))
h, err := handler.NewRotateFile(logfile, handler.EveryMinute, handler.WithMaxSize(128))
assert.NoErr(t, err)
assert.True(t, fsutil.IsFile(logfile))
l := slog.NewWithHandlers(h)
l.ReportCaller = true
for i := 0; i < 3; i++ {
l.Info("info", "message", i)
l.Warn("warn message", i)
}
l.MustClose()
// by time
logfile = "./testdata/both-rotate-bytime.log"
assert.NoErr(t, fsutil.DeleteIfFileExist(logfile))
h = handler.MustRotateFile(logfile, handler.EverySecond)
assert.True(t, fsutil.IsFile(logfile))
l = slog.NewWithHandlers(h)
for i := 0; i < 3; i++ {
l.Info("info", "message", i)
l.Warn("warn message", i)
fmt.Println("second ", i+1)
time.Sleep(time.Second * 1)
}
l.Error("error message")
assert.NoErr(t, l.FlushAll())
}
func TestNewSizeRotateFileHandler(t *testing.T) {
t.Run("NewSizeRotateFile", func(t *testing.T) {
logfile := "./testdata/size-rotate-file.log"
assert.NoErr(t, fsutil.DeleteIfFileExist(logfile))
h, err := handler.NewSizeRotateFile(logfile, 468, handler.WithBuffSize(256))
assert.NoErr(t, err)
assert.True(t, fsutil.IsFile(logfile))
l := slog.NewWithHandlers(h)
l.ReportCaller = true
l.CallerFlag = slog.CallerFlagFull
for i := 0; i < 4; i++ {
l.Info("this is a info", "message, index=", i)
l.Warn("this is a warn message, index=", i)
}
assert.NoErr(t, l.Close())
checkLogFileContents(t, logfile)
})
t.Run("MustSizeRotateFile", func(t *testing.T) {
logfile := "./testdata/must-size-rotate-file.log"
h := handler.MustSizeRotateFile(logfile, 128, handler.WithBuffSize(128))
h.SetFormatter(slog.NewJSONFormatter())
err := h.Handle(newLogRecord("this is a info message"))
assert.NoErr(t, err)
files := fsutil.Glob(logfile + "*")
assert.Len(t, files, 2)
})
}
func TestNewTimeRotateFileHandler_EveryDay(t *testing.T) {
logfile := "./testdata/time-rotate_EveryDay.log"
newFile := logfile + ".20221116"
clock := rotatefile.NewMockClock("2022-11-16 23:59:57")
options := []handler.ConfigFn{
handler.WithBuffSize(128),
handler.WithTimeClock(clock),
}
h := handler.MustTimeRotateFile(logfile, handler.EveryDay, options...)
assert.True(t, fsutil.IsFile(logfile))
l := slog.NewWithHandlers(h)
l.ReportCaller = true
l.TimeClock = clock.Now
for i := 0; i < 6; i++ {
l.WithData(sampleData).Info("the th:", i, "info message")
l.Warnf("the th:%d warn message text", i)
fmt.Println("log number ", (i+1)*2)
clock.Add(time.Second * 1)
}
l.MustClose()
checkLogFileContents(t, logfile)
checkLogFileContents(t, newFile)
}
func TestNewTimeRotateFileHandler_EveryHour(t *testing.T) {
clock := rotatefile.NewMockClock("2022-04-28 20:59:58")
logfile := "./testdata/time-rotate_EveryHour.log"
newFile := logfile + timex.DateFormat(clock.Now(), ".Ymd_H00")
options := []handler.ConfigFn{
handler.WithTimeClock(clock),
handler.WithBuffSize(0),
}
h, err := handler.NewTimeRotateFile(logfile, rotatefile.EveryHour, options...)
assert.NoErr(t, err)
assert.True(t, fsutil.IsFile(logfile))
l := slog.NewWithHandlers(h)
l.ReportCaller = true
l.TimeClock = clock.Now
for i := 0; i < 6; i++ {
l.WithData(sampleData).Info("the th:", i, "info message")
l.Warnf("the th:%d warn message text", i)
fmt.Println("log number ", (i+1)*2)
clock.Add(time.Second * 1)
}
l.MustClose()
checkLogFileContents(t, logfile)
checkLogFileContents(t, newFile)
}
func TestNewTimeRotateFileHandler_someSeconds(t *testing.T) {
logfile := "./testdata/time-rotate-Seconds.log"
assert.NoErr(t, fsutil.DeleteIfExist(logfile))
h, err := handler.NewTimeRotateFileHandler(logfile, handler.EverySecond)
assert.NoErr(t, err)
assert.True(t, fsutil.IsFile(logfile))
l := slog.NewWithHandlers(h)
l.ReportCaller = true
for i := 0; i < 3; i++ {
l.Info("info", "message", i)
l.Warn("warn message", i)
fmt.Println("second ", i+1)
time.Sleep(time.Second * 1)
}
l.MustClose()
// assert.NoErr(t, os.Remove(fpath))
}
func checkLogFileContents(t *testing.T, logfile string) {
assert.True(t, fsutil.IsFile(logfile))
bts, err := os.ReadFile(logfile)
assert.NoErr(t, err)
str := string(bts)
assert.Contains(t, str, "[INFO]")
assert.Contains(t, str, "info message")
assert.Contains(t, str, "[WARN]")
assert.Contains(t, str, "warn message")
}