-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler_test.go
More file actions
392 lines (336 loc) · 11 KB
/
handler_test.go
File metadata and controls
392 lines (336 loc) · 11 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
package log
import (
"bytes"
"context"
"encoding/json"
"log/slog"
"testing"
"github.com/go-coldbrew/log/loggers"
)
// parseJSON parses a JSON log line from a buffer and resets it.
func parseJSON(t *testing.T, buf *bytes.Buffer) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
t.Fatalf("failed to parse JSON log: %v\nraw: %s", err, buf.String())
}
buf.Reset()
return m
}
// setDefaultForTest sets the global ColdBrew handler and restores the
// previous state (both defaultHandler and slog.Default) on cleanup.
func setDefaultForTest(t *testing.T, h *Handler) {
t.Helper()
prevSlog := slog.Default()
prevHandler := defaultHandler.Load()
SetDefault(h)
t.Cleanup(func() {
slog.SetDefault(prevSlog)
defaultHandler.Store(prevHandler)
})
}
func TestNativeSlog_ContextFieldInjection(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := context.Background()
ctx = AddToContext(ctx, "trace_id", "abc-123")
ctx = AddToContext(ctx, "service", "test-svc")
slog.InfoContext(ctx, "hello world")
m := parseJSON(t, &buf)
if m["trace_id"] != "abc-123" {
t.Errorf("expected trace_id=abc-123, got %v", m["trace_id"])
}
if m["service"] != "test-svc" {
t.Errorf("expected service=test-svc, got %v", m["service"])
}
if m["msg"] != "hello world" {
t.Errorf("expected msg=hello world, got %v", m["msg"])
}
}
func TestNativeSlog_OverrideLogLevel(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithLevel(loggers.InfoLevel), loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
// Without override: debug should be filtered.
slog.DebugContext(context.Background(), "should be filtered")
if buf.Len() > 0 {
t.Errorf("expected debug to be filtered, got: %s", buf.String())
}
// With override: debug should pass through.
ctx := OverrideLogLevel(context.Background(), loggers.DebugLevel)
slog.DebugContext(ctx, "should appear")
if buf.Len() == 0 {
t.Error("expected debug message with override to appear")
}
m := parseJSON(t, &buf)
if m["msg"] != "should appear" {
t.Errorf("expected msg=should appear, got %v", m["msg"])
}
}
func TestHandler_WithAttrsPreservesContextInjection(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
wrapped := h.WithAttrs([]slog.Attr{slog.String("static_key", "static_val")})
ctx := context.Background()
ctx = AddToContext(ctx, "trace_id", "xyz-789")
r := slog.Record{}
r.Level = slog.LevelInfo
r.Message = "test with attrs"
err := wrapped.Handle(ctx, r)
if err != nil {
t.Fatalf("Handle returned error: %v", err)
}
m := parseJSON(t, &buf)
if m["static_key"] != "static_val" {
t.Errorf("expected static_key=static_val, got %v", m["static_key"])
}
if m["trace_id"] != "xyz-789" {
t.Errorf("expected trace_id=xyz-789 from context, got %v", m["trace_id"])
}
}
func TestHandler_WithGroupPreservesContextInjection(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
grouped := h.WithGroup("app")
ctx := context.Background()
ctx = AddToContext(ctx, "trace_id", "grp-456")
r := slog.Record{}
r.Level = slog.LevelInfo
r.Message = "grouped message"
r.AddAttrs(slog.String("action", "deploy"))
err := grouped.Handle(ctx, r)
if err != nil {
t.Fatalf("Handle returned error: %v", err)
}
m := parseJSON(t, &buf)
if m["msg"] != "grouped message" {
t.Errorf("expected msg=grouped message, got %v", m["msg"])
}
appGroup, _ := m["app"].(map[string]any)
if appGroup == nil {
t.Fatal("expected app group in output")
}
if appGroup["trace_id"] != "grp-456" {
t.Errorf("expected app.trace_id=grp-456 from context, got %v", appGroup["trace_id"])
}
}
func TestHandler_EmptyWithAttrs(t *testing.T) {
inner := slog.NewJSONHandler(&bytes.Buffer{}, nil)
h := NewHandlerWithInner(inner)
h2 := h.WithAttrs(nil)
if h2 != h {
t.Error("expected same handler for empty WithAttrs")
}
}
func TestHandler_EmptyWithGroup(t *testing.T) {
inner := slog.NewJSONHandler(&bytes.Buffer{}, nil)
h := NewHandlerWithInner(inner)
h2 := h.WithGroup("")
if h2 != h {
t.Error("expected same handler for empty WithGroup")
}
}
func TestHandler_Inner(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, nil)
h := NewHandlerWithInner(inner)
if h.Inner() != inner {
t.Error("Inner() should return the inner handler")
}
}
func TestHandler_SetGetLevel(t *testing.T) {
h := NewHandler()
h.SetLevel(loggers.DebugLevel)
if h.GetLevel() != loggers.DebugLevel {
t.Errorf("expected DebugLevel, got %v", h.GetLevel())
}
h.SetLevel(loggers.ErrorLevel)
if h.GetLevel() != loggers.ErrorLevel {
t.Errorf("expected ErrorLevel, got %v", h.GetLevel())
}
}
func TestCBLogger_ArgParsing(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
l := &cbLogger{handler: h}
// Test odd-leading form: first arg is message.
l.Info(context.Background(), "hello", "key1", "val1")
m := parseJSON(t, &buf)
if m["msg"] != "hello" {
t.Errorf("expected msg=hello, got %v", m["msg"])
}
if m["key1"] != "val1" {
t.Errorf("expected key1=val1, got %v", m["key1"])
}
// Test even-length with explicit "msg" key.
l.Info(context.Background(), "msg", "explicit message", "key2", "val2")
m = parseJSON(t, &buf)
if m["msg"] != "explicit message" {
t.Errorf("expected msg=explicit message, got %v", m["msg"])
}
if m["key2"] != "val2" {
t.Errorf("expected key2=val2, got %v", m["key2"])
}
// Test single arg.
l.Info(context.Background(), "just a message")
m = parseJSON(t, &buf)
if m["msg"] != "just a message" {
t.Errorf("expected msg=just a message, got %v", m["msg"])
}
}
func TestSetDefault_EnablesNativeSlog(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
slog.Info("native slog call", "key", "value")
m := parseJSON(t, &buf)
if m["msg"] != "native slog call" {
t.Errorf("expected msg=native slog call, got %v", m["msg"])
}
if m["key"] != "value" {
t.Errorf("expected key=value, got %v", m["key"])
}
}
func TestToAttr_SlogAttr(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := AddAttrsToContext(context.Background(), slog.String("user_id", "42"))
slog.InfoContext(ctx, "test")
m := parseJSON(t, &buf)
if m["user_id"] != "42" {
t.Errorf("expected user_id=42, got %v", m["user_id"])
}
}
func TestToAttr_SlogAttr_MapKeyIgnored(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := context.Background()
ctx = loggers.AddToLogContext(ctx, "ignored_key", slog.String("real_key", "value"))
slog.InfoContext(ctx, "test")
m := parseJSON(t, &buf)
if m["real_key"] != "value" {
t.Errorf("expected real_key=value, got %v", m["real_key"])
}
if _, exists := m["ignored_key"]; exists {
t.Errorf("did not expect ignored_key in output, got %v", m["ignored_key"])
}
}
func TestToAttr_SlogValue(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := context.Background()
ctx = loggers.AddToLogContext(ctx, "count", slog.IntValue(99))
slog.InfoContext(ctx, "test")
m := parseJSON(t, &buf)
if m["count"] != float64(99) {
t.Errorf("expected count=99, got %v", m["count"])
}
}
func TestAddAttrsToContext_Multiple(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := AddAttrsToContext(context.Background(),
slog.String("trace_id", "abc-123"),
slog.Int("user_id", 42),
)
slog.InfoContext(ctx, "handled")
m := parseJSON(t, &buf)
if m["trace_id"] != "abc-123" {
t.Errorf("expected trace_id=abc-123, got %v", m["trace_id"])
}
if m["user_id"] != float64(42) {
t.Errorf("expected user_id=42, got %v", m["user_id"])
}
}
func TestAddAttrsToContext_Overwrites(t *testing.T) {
var buf bytes.Buffer
inner := slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
h := NewHandlerWithInner(inner, loggers.WithCallerInfo(false))
setDefaultForTest(t, h)
ctx := AddToContext(context.Background(), "trace_id", "old-value")
ctx = AddAttrsToContext(ctx, slog.String("trace_id", "new-value"))
slog.InfoContext(ctx, "test")
m := parseJSON(t, &buf)
if m["trace_id"] != "new-value" {
t.Errorf("expected trace_id=new-value, got %v", m["trace_id"])
}
}
func TestAddAttrsToContext_EmptyKey(t *testing.T) {
ctx := AddAttrsToContext(context.Background(), slog.Attr{Key: "", Value: slog.StringValue("skip")})
fields := loggers.FromContext(ctx)
if fields != nil {
found := false
fields.Range(func(k, _ any) bool {
if k == "" {
found = true
}
return true
})
if found {
t.Error("expected empty-key attr to be skipped")
}
}
}
func TestAddAttrsToContext_Nil(t *testing.T) {
ctx := AddAttrsToContext(nil, slog.String("key", "val")) //nolint:staticcheck // testing nil context
if ctx == nil {
t.Error("expected non-nil context")
}
}
func TestDefaultIsSet(t *testing.T) {
prevHandler := defaultHandler.Load()
prevFlag := defaultExplicitlySet.Load()
t.Cleanup(func() {
defaultHandler.Store(prevHandler)
defaultExplicitlySet.Store(prevFlag)
})
// Reset state.
defaultHandler.Store(nil)
defaultExplicitlySet.Store(false)
// Lazy init via GetHandler does NOT set the flag.
_ = GetHandler()
if DefaultIsSet() {
t.Error("expected DefaultIsSet=false after lazy init")
}
// Explicit SetDefault sets the flag.
defaultHandler.Store(nil)
defaultExplicitlySet.Store(false)
SetDefault(NewHandler())
if !DefaultIsSet() {
t.Error("expected DefaultIsSet=true after SetDefault")
}
}
func TestLevelMapping(t *testing.T) {
tests := []struct {
cb loggers.Level
slog slog.Level
}{
{loggers.DebugLevel, slog.LevelDebug},
{loggers.InfoLevel, slog.LevelInfo},
{loggers.WarnLevel, slog.LevelWarn},
{loggers.ErrorLevel, slog.LevelError},
}
for _, tt := range tests {
if got := ToSlogLevel(tt.cb); got != tt.slog {
t.Errorf("ToSlogLevel(%v) = %v, want %v", tt.cb, got, tt.slog)
}
if got := FromSlogLevel(tt.slog); got != tt.cb {
t.Errorf("FromSlogLevel(%v) = %v, want %v", tt.slog, got, tt.cb)
}
}
}