-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathissues_test.go
232 lines (194 loc) · 6.44 KB
/
issues_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package slog_test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/gookit/goutil/byteutil"
"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"
)
// https://github.com/gookit/slog/issues/27
func TestIssues_27(t *testing.T) {
defer slog.Reset()
count := 0
for {
if count >= 6 {
break
}
slog.Infof("info log %d", count)
time.Sleep(time.Second)
count++
}
}
// https://github.com/gookit/slog/issues/31
func TestIssues_31(t *testing.T) {
defer slog.Reset()
defer slog.MustClose()
// slog.DangerLevels equals slog.Levels{slog.PanicLevel, slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel}
h1 := handler.MustFileHandler("testdata/error_issue31.log", handler.WithLogLevels(slog.DangerLevels))
infoLevels := slog.Levels{slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel}
h2 := handler.MustFileHandler("testdata/info_issue31.log", handler.WithLogLevels(infoLevels))
slog.PushHandler(h1)
slog.PushHandlers(h2)
// add logs
slog.Info("info message text")
slog.Error("error message text")
}
// https://github.com/gookit/slog/issues/52
func TestIssues_52(t *testing.T) {
testTemplate := "[{{datetime}}] [{{level}}] {{message}} {{data}} {{extra}}"
slog.SetLogLevel(slog.ErrorLevel)
slog.GetFormatter().(*slog.TextFormatter).SetTemplate(testTemplate)
slog.Error("Error message")
slog.Reset()
fmt.Println()
// dump.P(slog.GetFormatter())
}
// https://github.com/gookit/slog/issues/75
func TestIssues_75(t *testing.T) {
slog.Error("Error message 1")
// set max level
slog.SetLogLevel(slog.Level(0))
// slog.SetLogLevel(slog.PanicLevel)
slog.Error("Error message 2")
slog.Reset()
// dump.P(slog.GetFormatter())
}
// https://github.com/gookit/slog/issues/105
func TestIssues_105(t *testing.T) {
t.Run("simple write", func(t *testing.T) {
for i := 0; i < 10; i++ {
slog.Error("simple error log", i)
time.Sleep(time.Millisecond * 100)
}
})
// test concurrent write
t.Run("concurrent write", func(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
slog.Error("concurrent error log", i)
time.Sleep(time.Millisecond * 100)
wg.Done()
}(i)
}
wg.Wait()
})
}
// https://github.com/gookit/slog/issues/108
func TestIssues_108(t *testing.T) {
buf1 := byteutil.NewBuffer()
root := slog.NewWithName("root", func(l *slog.Logger) {
l.ChannelName = l.Name()
l.AddHandler(handler.NewSimple(buf1, slog.InfoLevel))
})
root.Info("root info message")
root.Warn("root warn message")
str := buf1.ResetGet()
fmt.Println(str)
assert.StrContains(t, str, "[root] [INFO")
assert.StrContains(t, str, "[root] [WARN")
buf2 := byteutil.NewBuffer()
probe := slog.NewWithName("probe", func(l *slog.Logger) {
l.ChannelName = l.Name()
l.AddHandler(handler.NewSimple(buf2, slog.InfoLevel))
})
probe.Info("probe info message")
probe.Warn("probe warn message")
str = buf2.ResetGet()
fmt.Println(str)
assert.StrContains(t, str, "[probe] [INFO")
assert.StrContains(t, str, "[probe] [WARN")
}
// https://github.com/gookit/slog/issues/139
// 自定义模板报 invalid memory address or nil pointer dereference #139
func TestIssues_139(t *testing.T) {
myTemplate := "[{{datetime}}] [{{requestid}}] [{{level}}] {{message}}\n"
textFormatter := &slog.TextFormatter{TimeFormat: "2006-01-02 15:04:05.000"}
textFormatter.SetTemplate(myTemplate)
// use func create
// textFormatter := slog.NewTextFormatter(myTemplate).Configure(func(f *slog.TextFormatter) {
// f.TimeFormat = "2006-01-02 15:04:05.000"
// })
h1 := handler.NewConsoleHandler(slog.AllLevels)
h1.SetFormatter(textFormatter)
L := slog.New()
L.AddHandlers(h1)
// add processor <====
// L.AddProcessor(slog.ProcessorFunc(func(r *slog.Record) {
// r.Fields["requestid"] = r.Ctx.Value("requestid")
// }))
L.AddProcessor(slog.AppendCtxKeys("requestid"))
ctx := context.WithValue(context.Background(), "requestid", "111111")
L.WithCtx(ctx).Info("test")
}
// https://github.com/gookit/slog/issues/121
// 当我配置按日期的方式来滚动日志时,当大于 1 天时只能按 1 天来滚动日志。
func TestIssues_121(t *testing.T) {
seconds := timex.OneDaySec * 7 // 7天
logFile := "testdata/issue121_7day.log"
clock := rotatefile.NewMockClock("2024-03-25 08:04:02")
fh, err := handler.NewTimeRotateFileHandler(
logFile,
rotatefile.RotateTime(seconds),
handler.WithLogLevels(slog.NormalLevels),
handler.WithBuffSize(128),
handler.WithBackupNum(20),
handler.WithTimeClock(clock),
handler.WithDebugMode, // debug mode
// handler.WithCompress(log.compress),
// handler.WithFilePerm(log.filePerm),
)
assert.NoError(t, err)
// create logger with handler and clock.
l := slog.NewWithHandlers(fh).Config(func(sl *slog.Logger) {
sl.TimeClock = clock.Now
})
// add logs
for i := 0; i < 50; i++ {
l.Infof("hi, this is a exmple information ... message text. log index=%d", i)
clock.Add(24 * timex.Hour)
}
l.MustClose()
}
// https://github.com/gookit/slog/issues/137
// 按日期滚动 如果当天时间节点的日志文件已存在 不会append 会直接替换 #137
func TestIssues_137(t *testing.T) {
logFile := "testdata/issue137_case.log"
fsutil.MustSave(logFile, "hello, this is a log file content\n")
l := slog.NewWithHandlers(handler.MustFileHandler(logFile))
// add logs
for i := 0; i < 5; i++ {
l.Infof("hi, this is a example information ... message text. log index=%d", i)
}
l.MustClose()
// read file content
content := fsutil.ReadString(logFile)
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")
}