Skip to content

Commit

Permalink
Do not print empty Attr
Browse files Browse the repository at this point in the history
This is described in Handler.Handle method: If an Attr's key and value are both the zero value, ignore the Attr.
  • Loading branch information
injeniero committed Nov 10, 2023
1 parent 500573d commit a125e81
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (e encoder) writeMessage(buf *buffer, level slog.Level, msg string) {
}

func (e encoder) writeAttr(buf *buffer, a slog.Attr, group string) {
// Elide empty Attrs.
if a.Equal(slog.Attr{}) {
return
}
value := a.Value.Resolve()
if value.Kind() == slog.KindGroup {
subgroup := a.Key
Expand Down
2 changes: 2 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestHandler_Attr(t *testing.T) {
slog.Any("err", errors.New("the error")),
slog.Any("stringer", theStringer{}),
slog.Any("nostringer", noStringer{Foo: "bar"}),
slog.Attr{},
slog.Any("", nil),
)
AssertNoError(t, h.Handle(context.Background(), rec))

Expand Down

0 comments on commit a125e81

Please sign in to comment.