Skip to content

Commit 724d7fa

Browse files
committed
zapslog: Test with slogtest
Adds a test for zapslog that verifies its behavior with slogtest's TestHandler function. This verifies compliance with the slog logging contract. Resolves #1334
1 parent 55a2367 commit 724d7fa

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

exp/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
)
99

1010
require (
11+
github.com/benbjohnson/clock v1.1.0 // indirect
1112
github.com/davecgh/go-spew v1.1.1 // indirect
1213
github.com/pmezard/go-difflib v1.0.0 // indirect
1314
go.uber.org/atomic v1.10.0 // indirect

exp/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
2+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
23
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
45
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

exp/zapslog/handler_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323
package zapslog
2424

2525
import (
26+
"bytes"
27+
"encoding/json"
2628
"log/slog"
2729
"testing"
30+
"testing/slogtest"
2831

2932
"github.com/stretchr/testify/assert"
3033
"github.com/stretchr/testify/require"
3134
"go.uber.org/zap/zapcore"
35+
"go.uber.org/zap/zaptest"
3236
"go.uber.org/zap/zaptest/observer"
3337
)
3438

@@ -48,3 +52,41 @@ func TestAddSource(t *testing.T) {
4852
"Unexpected caller annotation.",
4953
)
5054
}
55+
56+
func TestSlogtest(t *testing.T) {
57+
var buff bytes.Buffer
58+
core := zapcore.NewCore(
59+
zapcore.NewJSONEncoder(zapcore.EncoderConfig{
60+
TimeKey: slog.TimeKey,
61+
MessageKey: slog.MessageKey,
62+
LevelKey: slog.LevelKey,
63+
EncodeLevel: zapcore.CapitalLevelEncoder,
64+
EncodeTime: zapcore.RFC3339TimeEncoder,
65+
}),
66+
zapcore.AddSync(&buff),
67+
zapcore.DebugLevel,
68+
)
69+
70+
// zaptest doesn't expose the underlying core,
71+
// so we'll extract it from the logger.
72+
testCore := zaptest.NewLogger(t).Core()
73+
74+
handler := NewHandler(zapcore.NewTee(core, testCore), nil /* options */)
75+
err := slogtest.TestHandler(
76+
handler,
77+
func() []map[string]any {
78+
// Parse the newline-delimted JSON in buff.
79+
var entries []map[string]any
80+
81+
dec := json.NewDecoder(bytes.NewReader(buff.Bytes()))
82+
for dec.More() {
83+
var ent map[string]any
84+
require.NoError(t, dec.Decode(&ent), "Error decoding log message")
85+
entries = append(entries, ent)
86+
}
87+
88+
return entries
89+
},
90+
)
91+
require.NoError(t, err, "Unexpected error from slogtest.TestHandler")
92+
}

0 commit comments

Comments
 (0)