Debian Codegraph @

logger_test.go

raw symbols SCIP
1 package clog
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "log/slog"
9 "reflect"
10 "testing"
11 )
12
13 var (
14 testopts = &slog.HandlerOptions{
15 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
16 // Ignore time to make testing easier.
17 if a.Key == "time" {
18 return slog.Attr{}
19 }
20 return a
21 },
22 }
23 )
24
25 func TestLogger(t *testing.T) {
26 ctx := context.Background()
27 b := new(bytes.Buffer)
28 base := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
29 log := NewLogger(base).With("a", "b")
30 log.InfoContext(ctx, "")
31 t.Log(b.String())
32
33 want := map[string]any{
34 "level": "INFO",
35 "msg": "",
36 "a": "b",
37 }
38
39 var got map[string]any
40 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
41 t.Fatal(err)
42 }
43 if !reflect.DeepEqual(want, got) {
44 t.Errorf("want %v, got %v", want, got)
45 }
46 }
47
48 func TestLoggerNilBase(t *testing.T) {
49 log := NewLogger(nil)
50 log.Info("")
51 }
52
53 func TestLoggerFromContext(t *testing.T) {
54 b := new(bytes.Buffer)
55 base := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
56 log := NewLogger(base).With("a", "b")
57
58 ctx := WithLogger(context.Background(), log)
59 FromContext(ctx).Info("")
60
61 want := map[string]any{
62 "level": "INFO",
63 "msg": "",
64 "a": "b",
65 }
66
67 t.Run("FromContext.Info", func(t *testing.T) {
68 var got map[string]any
69 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
70 t.Fatal(err)
71 }
72 if !reflect.DeepEqual(want, got) {
73 t.Errorf("want %v, got %v", want, got)
74 }
75 })
76
77 b.Reset()
78
79 t.Run("clog.Info", func(t *testing.T) {
80 InfoContext(ctx, "")
81 var got map[string]any
82 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
83 t.Fatal(err)
84 }
85 if !reflect.DeepEqual(want, got) {
86 t.Errorf("want %v, got %v", want, got)
87 }
88 })
89 }
90
91 func TestLoggerPC(t *testing.T) {
92 b := new(bytes.Buffer)
93 log := NewLogger(slog.New(NewHandler(slog.NewJSONHandler(b, &slog.HandlerOptions{
94 AddSource: true,
95 ReplaceAttr: testopts.ReplaceAttr,
96 }))))
97
98 log.Info("")
99 t.Log(b.String())
100
101 var got struct {
102 Source struct {
103 File string `json:"file"`
104 Function string `json:"function"`
105 } `json:"source"`
106 }
107 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
108 t.Fatal(err)
109 }
110 // Knowing that the PC is from this test is good enough.
111 want := fmt.Sprintf("github.com/chainguard-dev/clog.%s", t.Name())
112 if got.Source.Function != want {
113 t.Errorf("want %v, got %v", want, got)
114 }
115 }
116
117 func TestWith(t *testing.T) {
118 ctx := context.WithValue(context.Background(), "test", "test")
119 log := NewLoggerWithContext(ctx, nil)
120 withed := log.With("a", "b")
121 if want := withed.ctx; want != ctx {
122 t.Errorf("want %v, got %v", want, ctx)
123 }
124 withed = log.WithGroup("a")
125 if want := withed.ctx; want != ctx {
126 t.Errorf("want %v, got %v", want, ctx)
127 }
128 }
129
130 func TestDefaultHandler(t *testing.T) {
131 old := slog.Default()
132 defer func() {
133 slog.SetDefault(old)
134 }()
135
136 b := new(bytes.Buffer)
137 slog.SetDefault(slog.New(slog.NewJSONHandler(b, testopts)))
138
139 t.Run("Info", func(t *testing.T) {
140 FromContext(WithValues(context.Background(), "a", "b")).Info("")
141 want := map[string]any{
142 "level": "INFO",
143 "msg": "",
144 "a": "b",
145 }
146 var got map[string]any
147 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
148 t.Fatal(err)
149 }
150 if !reflect.DeepEqual(want, got) {
151 t.Errorf("want %v, got %v", want, got)
152 }
153 })
154
155 b.Reset()
156
157 t.Run("InfoContext", func(t *testing.T) {
158 // Set logger with original value
159 ctx := WithValues(context.Background(), "a", "b")
160 logger := FromContext(ctx)
161
162 // Override value in request context - we expect this to overwrite the original value set in the logger
163 logger.InfoContext(WithValues(ctx, "a", "c"), "")
164
165 want := map[string]any{
166 "level": "INFO",
167 "msg": "",
168 "a": "c",
169 }
170 var got map[string]any
171 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
172 t.Fatal(err)
173 }
174 if !reflect.DeepEqual(want, got) {
175 t.Errorf("want %v, got %v", want, got)
176 }
177 })
178
179 b.Reset()
180
181 t.Run("Debug - no log", func(t *testing.T) {
182 logger := FromContext(context.Background())
183 logger.Debug("asdf")
184
185 if b.Len() != 0 {
186 t.Errorf("want empty, got %q", b.String())
187 }
188 })
189 }
190
191 func TestContext(t *testing.T) {
192 old := slog.Default()
193 t.Cleanup(func() {
194 slog.SetDefault(old)
195 })
196
197 b := new(bytes.Buffer)
198 slog.SetDefault(slog.New(slog.NewJSONHandler(b, testopts)))
199
200 msg := "hello world"
201 want := map[string]any{
202 "level": "INFO",
203 "msg": msg,
204 "a": "b",
205 }
206
207 ctx := WithValues(context.Background(), "a", "b")
208 // These should all give the same output and be functionally equivalent.
209 for _, tc := range []struct {
210 name string
211 fn func()
212 }{
213 {
214 name: "clog.InfoContext",
215 fn: func() {
216 InfoContext(ctx, msg)
217 },
218 },
219 {
220 name: "clog.FromContext.Info",
221 fn: func() {
222 FromContext(ctx).Info(msg)
223 },
224 },
225 {
226 name: "clog.FromContext.InfoContext",
227 fn: func() {
228 FromContext(ctx).InfoContext(ctx, msg)
229 },
230 },
231 } {
232 t.Run(tc.name, func(t *testing.T) {
233 b.Reset()
234 tc.fn()
235
236 var got map[string]any
237 if err := json.Unmarshal(b.Bytes(), &got); err != nil {
238 t.Fatal(err)
239 }
240 if !reflect.DeepEqual(want, got) {
241 t.Errorf("want %v, got %v", want, got)
242 }
243 })
244 }
245 }