Skip to content

Commit a125e81

Browse files
committed
Do not print empty Attr
This is described in Handler.Handle method: If an Attr's key and value are both the zero value, ignore the Attr.
1 parent 500573d commit a125e81

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

encoding.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func (e encoder) writeMessage(buf *buffer, level slog.Level, msg string) {
9797
}
9898

9999
func (e encoder) writeAttr(buf *buffer, a slog.Attr, group string) {
100+
// Elide empty Attrs.
101+
if a.Equal(slog.Attr{}) {
102+
return
103+
}
100104
value := a.Value.Resolve()
101105
if value.Kind() == slog.KindGroup {
102106
subgroup := a.Key

handler_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func TestHandler_Attr(t *testing.T) {
6262
slog.Any("err", errors.New("the error")),
6363
slog.Any("stringer", theStringer{}),
6464
slog.Any("nostringer", noStringer{Foo: "bar"}),
65+
slog.Attr{},
66+
slog.Any("", nil),
6567
)
6668
AssertNoError(t, h.Handle(context.Background(), rec))
6769

0 commit comments

Comments
 (0)