-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslogx_test.go
More file actions
420 lines (325 loc) · 11.4 KB
/
slogx_test.go
File metadata and controls
420 lines (325 loc) · 11.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package slogx_test
import (
"context"
"log/slog"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/yylego/slogx"
)
// setupTestLogger creates a JSON log instance in testing
// setupTestLogger 创建测试用 JSON 日志实例
func setupTestLogger() *slog.Logger {
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))
}
// =============== Basic structure and constructor tests ===============
// =============== 基础结构和构造函数测试 ===============
// TestNew tests the New function
// TestNew 测试 New 函数
func TestNew(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
require.NotNil(t, g)
require.Panics(t, func() {
slogx.New(nil)
})
}
// TestWithCallerSkip tests the WithCallerSkip function
// TestWithCallerSkip 测试 WithCallerSkip 功能
func TestWithCallerSkip(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
help := func(msg string) {
g.Info(msg + " (no skip)")
g.WithCallerSkip(1).Info(msg + " (with skip)")
}
help("caller skip test")
a := func(msg string) {
g.WithCallerSkip(2).Info(msg + " (nested skip)")
}
b := func(msg string) {
a(msg)
}
b("multi-level skip test")
}
// TestSkip tests the Skip method in detail
// TestSkip 详细测试 Skip 方法
func TestSkip(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
help := func(msg string) {
g.Info(msg + " (no skip)")
g.Skip(1).Info(msg + " (Skip method)")
g.WithCallerSkip(1).Info(msg + " (WithCallerSkip method)")
}
help("skip method test")
a := g.Skip(2)
b := g.WithCallerSkip(2)
// Both should have same skip value inside
// 两者内部的 skip 值应该相同
require.Equal(t, a.Handler(), b.Handler())
}
// =============== slog-compatible no-context version tests ===============
// =============== slog 兼容的无上下文版本测试 ===============
// TestBasicLoggingMethods tests Debug, Info, Warn, Error methods
// TestBasicLoggingMethods 测试 Debug, Info, Warn, Error 方法
func TestBasicLoggingMethods(t *testing.T) {
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
l.Debug("debug message", "key", "debug_value")
l.Info("info message", "key", "info_value")
l.Warn("warn message", "key", "warn_value")
l.Error("error message", "key", "error_value")
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.Debug("debug message", "key", "debug_value")
g.Info("info message", "key", "info_value")
g.Warn("warn message", "key", "warn_value")
g.Error("error message", "key", "error_value")
})
}
// =============== slog-compatible context version tests ===============
// =============== slog 兼容的带上下文版本测试 ===============
// TestContextMethods tests DebugContext, InfoContext, WarnContext, ErrorContext methods
// TestContextMethods 测试 DebugContext, InfoContext, WarnContext, ErrorContext 方法
func TestContextMethods(t *testing.T) {
ctx := context.Background()
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
l.DebugContext(ctx, "debug context", "context", true)
l.InfoContext(ctx, "info context", "context", true)
l.WarnContext(ctx, "warn context", "context", true)
l.ErrorContext(ctx, "error context", "context", true)
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.DebugContext(ctx, "debug context", "context", true)
g.InfoContext(ctx, "info context", "context", true)
g.WarnContext(ctx, "warn context", "context", true)
g.ErrorContext(ctx, "error context", "context", true)
})
}
// =============== slog-compatible structured attribute version tests ===============
// =============== slog 兼容的结构化属性版本测试 ===============
// TestLogAndLogAttrs tests Log and LogAttrs methods
// TestLogAndLogAttrs 测试 Log 和 LogAttrs 方法
func TestLogAndLogAttrs(t *testing.T) {
ctx := context.Background()
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
l.Log(ctx, slog.LevelInfo, "generic log", "method", "Log")
l.LogAttrs(ctx, slog.LevelInfo, "structured log",
slog.String("method", "LogAttrs"),
slog.Int("number", 42))
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.Log(ctx, slog.LevelInfo, "generic log", "method", "Log")
g.LogAttrs(ctx, slog.LevelInfo, "structured log",
slog.String("method", "LogAttrs"),
slog.Int("number", 42))
})
}
// =============== slog-compatible With and support function tests ===============
// =============== slog 兼容的 With 和工具函数测试 ===============
// TestWith tests the With method
// TestWith 测试 With 方法
func TestWith(t *testing.T) {
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
a := l.With("service", "test", "version", "1.0")
a.Info("with fields test")
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
a := g.With("service", "test", "version", "1.0")
a.Info("with fields test")
b := g.With()
require.Equal(t, g.Handler(), b.Handler())
c := g.WithCallerSkip(1).With("preserved", true)
c.Info("skip preserved test")
})
}
// TestWithGroup tests the WithGroup method
// TestWithGroup 测试 WithGroup 方法
func TestWithGroup(t *testing.T) {
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
a := l.WithGroup("app")
a.Info("group test", "component", "auth")
b := l.With("request_id", "123").WithGroup("api")
b.Info("chained logger test")
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
a := g.WithGroup("app")
a.Info("group test", "component", "auth")
b := g.WithCallerSkip(0).With("request_id", "123").WithGroup("api")
b.Info("chained logger test")
})
}
// TestEnabled tests the Enabled method
// TestEnabled 测试 Enabled 方法
func TestEnabled(t *testing.T) {
ctx := context.Background()
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
require.True(t, l.Enabled(ctx, slog.LevelInfo))
require.False(t, l.Enabled(ctx, slog.LevelDebug-1))
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
require.True(t, g.Enabled(ctx, slog.LevelInfo))
require.False(t, g.Enabled(ctx, slog.LevelDebug-1))
})
}
// TestHandler tests the Handler method
// TestHandler 测试 Handler 方法
func TestHandler(t *testing.T) {
t.Run("StandardSlog", func(t *testing.T) {
x := setupTestLogger()
// Test Handler method with standard slog
// 使用标准 slog 测试 Handler 方法
h := x.Handler()
require.NotNil(t, h)
})
t.Run("SlogxWrapper", func(t *testing.T) {
x := setupTestLogger()
g := slogx.New(x)
// Test Handler method with slogx
// 使用 slogx 测试 Handler 方法
h := g.Handler()
require.NotNil(t, h)
// Check handle component matches the base (slogx should not change the handle component)
// 验证 handle 组件与原始的相同(slogx 不应修改 handle 组件)
o := x.Handler()
require.Equal(t, o, h)
})
}
// =============== Comprehensive function tests ===============
// =============== 综合功能测试 ===============
// TestSkipWithAllMethods tests Skip method with all logging methods
// TestSkipWithAllMethods 测试 Skip 方法与所有日志方法的配合
func TestSkipWithAllMethods(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
ctx := context.Background()
// Test Skip method with different log levels
// 测试 Skip 方法与不同日志级别
s := g.Skip(1)
s.Debug("debug with skip")
s.Info("info with skip")
s.Warn("warn with skip")
s.Error("error with skip")
s.DebugContext(ctx, "debug context with skip")
s.InfoContext(ctx, "info context with skip")
s.WarnContext(ctx, "warn context with skip")
s.ErrorContext(ctx, "error context with skip")
s.Log(ctx, slog.LevelInfo, "generic log with skip", "method", "Log")
s.LogAttrs(ctx, slog.LevelInfo, "structured log with skip",
slog.String("method", "LogAttrs"),
slog.Int("skip", 1))
a := g.Skip(1).With("test", "value")
a.Info("skip with chaining test")
b := g.Skip(1).WithGroup("test")
b.Info("skip group chaining test")
g.Skip(1).Skip(1).Info("double skip test", "total", 2)
}
// TestErrorHandling tests edge cases and error conditions
// TestErrorHandling 测试边缘情况和错误条件
func TestErrorHandling(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.Info("odd args test", "key1", "value1", "key2", "value2", "orphan")
g.Info("non-string keys test", 123, "numeric", true, "boolean", 3.14, "float")
}
// TestSkipEdgeCases tests edge cases with skip values
// TestSkipEdgeCases 测试 skip 值的边缘情况
func TestSkipEdgeCases(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.Skip(-1).Info("negative skip test")
g.Skip(0).Info("zero skip test")
g.Skip(100).Info("large skip test")
g.Skip(1).Skip(1).Info("chained skip test", "total", 2)
g.Skip(1).WithCallerSkip(1).Info("mixed skip methods test", "total", 2)
}
// TestSlogCompatibility ensures 1:1 API mapping with slog.Logger
// TestSlogCompatibility 确保与 slog.Logger 的 1:1 API 兼容性
func TestSlogCompatibility(t *testing.T) {
ctx := context.Background()
t.Run("StandardSlog", func(t *testing.T) {
l := setupTestLogger()
l.Debug("test", "key", "value")
l.Info("test", "key", "value")
l.Warn("test", "key", "value")
l.Error("test", "key", "value")
l.DebugContext(ctx, "test", "key", "value")
l.InfoContext(ctx, "test", "key", "value")
l.WarnContext(ctx, "test", "key", "value")
l.ErrorContext(ctx, "test", "key", "value")
l.Log(ctx, slog.LevelInfo, "test", "key", "value")
l.LogAttrs(ctx, slog.LevelInfo, "test", slog.String("key", "value"))
e := l.Enabled(ctx, slog.LevelInfo)
h := l.Handler()
require.True(t, e)
require.NotNil(t, h)
a := l.With("key", "value")
b := l.WithGroup("group")
require.NotEqual(t, l.Handler(), a.Handler())
require.NotEqual(t, l.Handler(), b.Handler())
})
t.Run("SlogxWrapper", func(t *testing.T) {
l := setupTestLogger()
g := slogx.New(l)
g.Debug("test", "key", "value")
g.Info("test", "key", "value")
g.Warn("test", "key", "value")
g.Error("test", "key", "value")
g.DebugContext(ctx, "test", "key", "value")
g.InfoContext(ctx, "test", "key", "value")
g.WarnContext(ctx, "test", "key", "value")
g.ErrorContext(ctx, "test", "key", "value")
g.Log(ctx, slog.LevelInfo, "test", "key", "value")
g.LogAttrs(ctx, slog.LevelInfo, "test", slog.String("key", "value"))
e := g.Enabled(ctx, slog.LevelInfo)
h := g.Handler()
require.True(t, e)
require.NotNil(t, h)
a := g.With("key", "value")
b := g.WithGroup("group")
require.NotEqual(t, g.Handler(), a.Handler())
require.NotEqual(t, g.Handler(), b.Handler())
c := g.WithCallerSkip(1)
c.Info("caller skip test")
s := g.Skip(1)
s.Info("skip method test")
})
}
// TestSugar tests Logger.Sugar() method
// TestSugar 测试 Logger.Sugar() 方法
func TestSugar(t *testing.T) {
logger := setupTestLogger()
g := slogx.New(logger)
sugar := g.Sugar()
require.NotNil(t, sugar)
// Test basic SugaredLogger function
// 测试基本 SugaredLogger 功能
sugar.Info("test", "message", 123)
sugar.Debug("1", 2, 3, 4, "5", 6) // Should be "12 3 456"
sugar.Debugln("1", 2, 3, 4, "5", 6) // Should be "1 2 3 4 5 6"
// Test chaining with other Logger methods
// 测试与其他 Logger 方法的链式调用
g.Skip(1).Sugar().Warn("chained", "skip", "sugar")
g.WithGroup("test").Sugar().Error("grouped", "sugar", "logging")
}