-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctx_test.go
126 lines (108 loc) · 4.86 KB
/
ctx_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
package slogctx
import (
"bytes"
"context"
"errors"
"log/slog"
"testing"
"github.com/veqryn/slog-context/internal/test"
)
func TestCtx(t *testing.T) {
buf := &bytes.Buffer{}
h := slog.NewJSONHandler(buf, &slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// fmt.Printf("ReplaceAttr: key:%s valueKind:%s value:%s nilGroups:%t groups:%#+v\n", a.Key, a.Value.Kind().String(), a.Value.String(), groups == nil, groups)
if groups == nil && a.Key == slog.TimeKey {
return slog.Time(slog.TimeKey, test.DefaultTime)
}
return a
},
})
// Confirm FromCtx retrieves default if nothing stored in ctx yet
l := slog.New(h)
slog.SetDefault(l) // Can cause issues in tests run in parallel, so don't use it in other tests, just this test
if FromCtx(nil) != l {
t.Error("Expecting default logger retrieved")
}
if FromCtx(context.Background()) != l {
t.Error("Expecting default logger retrieved")
}
ctx := NewCtx(nil, slog.Default())
ctx = With(ctx, "with1", "arg1", "with1", "arg2")
ctx = With(ctx, "with2", "arg1", "with2", "arg2")
With(ctx, "with3", "arg1", "with3", "arg2") // Ensure we aren't overwriting the parent context
WithGroup(ctx, "group0") // Ensure we aren't overwriting the parent context
ctx = WithGroup(ctx, "group1")
WithGroup(ctx, "group2") // Ensure we aren't overwriting the parent context
ctx = With(ctx, "with4", "arg1", "with4", "arg2")
ctx = With(ctx, "with5", "arg1", "with5", "arg2")
With(ctx, "with6", "arg1", "with6", "arg2") // Ensure we aren't overwriting the parent context
// Test with getting logger back out
l = FromCtx(ctx)
l.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedInfo := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","with1":"arg1","with1":"arg2","with2":"arg1","with2":"arg2","group1":{"with4":"arg1","with4":"arg2","with5":"arg1","with5":"arg2","main1":"arg1","main1":"arg2"}}
`
if buf.String() != expectedInfo {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedInfo, buf.String())
}
// Test with wrappers
buf.Reset()
Log(ctx, slog.LevelDebug-10, "ignored")
LogAttrs(ctx, slog.LevelDebug-10, "ignored")
if buf.String() != "" {
t.Errorf("Expected:\n%s\nGot:\n%s\n", "", buf.String())
}
buf.Reset()
Debug(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedDebug := `{"time":"2023-09-29T13:00:59Z","level":"DEBUG","msg":"main message","with1":"arg1","with1":"arg2","with2":"arg1","with2":"arg2","group1":{"with4":"arg1","with4":"arg2","with5":"arg1","with5":"arg2","main1":"arg1","main1":"arg2"}}
`
if buf.String() != expectedDebug {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedDebug, buf.String())
}
buf.Reset()
Info(ctx, "main message", "main1", "arg1", "main1", "arg2")
if buf.String() != expectedInfo {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedInfo, buf.String())
}
buf.Reset()
Warn(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedWarn := `{"time":"2023-09-29T13:00:59Z","level":"WARN","msg":"main message","with1":"arg1","with1":"arg2","with2":"arg1","with2":"arg2","group1":{"with4":"arg1","with4":"arg2","with5":"arg1","with5":"arg2","main1":"arg1","main1":"arg2"}}
`
if buf.String() != expectedWarn {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedWarn, buf.String())
}
buf.Reset()
Error(ctx, "main message", "main1", "arg1", "main1", "arg2", Err(errors.New("an error")))
expectedError := `{"time":"2023-09-29T13:00:59Z","level":"ERROR","msg":"main message","with1":"arg1","with1":"arg2","with2":"arg1","with2":"arg2","group1":{"with4":"arg1","with4":"arg2","with5":"arg1","with5":"arg2","main1":"arg1","main1":"arg2","err":"an error"}}
`
if buf.String() != expectedError {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedError, buf.String())
}
buf.Reset()
Log(ctx, slog.LevelWarn, "main message", "main1", "arg1", "main1", "arg2")
if buf.String() != expectedWarn {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedWarn, buf.String())
}
buf.Reset()
LogAttrs(ctx, slog.LevelInfo, "main message", slog.String("main1", "arg1"), slog.String("main1", "arg2"))
if buf.String() != expectedInfo {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedInfo, buf.String())
}
// Test with new context/nil
buf.Reset()
Log(nil, slog.LevelWarn, "main message", "main1", "arg1", "main1", "arg2")
expectedWarnNil := `{"time":"2023-09-29T13:00:59Z","level":"WARN","msg":"main message","main1":"arg1","main1":"arg2"}
`
if buf.String() != expectedWarnNil {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedWarnNil, buf.String())
}
buf.Reset()
LogAttrs(nil, slog.LevelInfo, "main message", slog.String("main1", "arg1"), slog.String("main1", "arg2"))
expectedInfoNil := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","main1":"arg1","main1":"arg2"}
`
if buf.String() != expectedInfoNil {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedInfoNil, buf.String())
}
}