Skip to content

Commit 1b235b5

Browse files
authored
TestLogger logs at most specific level with sources (#188)
Also apply our custom mutator so the fields match.
1 parent 2f60b72 commit 1b235b5

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

logging/testing.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package logging
1717
import (
1818
"io"
1919
"log/slog"
20+
"math"
2021
"testing"
2122
)
2223

@@ -26,17 +27,24 @@ func TestLogger(tb testing.TB) *slog.Logger {
2627
tb.Helper()
2728

2829
w := &testingWriter{tb}
29-
return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{
30+
31+
// Use the lowest possible level (aka log everything). Slog levels are
32+
// arbitrary integers, so by choosing the lowest possible integer, we will
33+
// catch every possible log.
34+
level := slog.Level(math.MinInt)
35+
36+
return slog.New(NewLevelHandler(level, slog.NewTextHandler(w, &slog.HandlerOptions{
3037
AddSource: true,
31-
Level: slog.LevelDebug,
38+
Level: level,
3239
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
3340
// Drop time key since the test failures will include timestamps
3441
if a.Key == slog.TimeKey {
3542
return slog.Attr{}
3643
}
37-
return a
44+
45+
return cloudLoggingAttrsEncoder()(groups, a)
3846
},
39-
}))
47+
})))
4048
}
4149

4250
var _ io.Writer = (*testingWriter)(nil)

0 commit comments

Comments
 (0)