Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (b *buffer) WriteTo(dst io.Writer) (int64, error) {
return int64(n), nil
}

func (b *buffer) Write(bt []byte) (int, error) {
*b = append(*b, bt...)
return len(bt), nil
}

func (b *buffer) Reset() {
*b = (*b)[:0]
}
Expand Down
8 changes: 7 additions & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ func (e encoder) writeValue(buf *buffer, value slog.Value) {
case slog.KindAny:
switch v := value.Any().(type) {
case error:
e.writeColoredString(buf, v.Error(), e.opts.Theme.AttrValueError())
if _, ok := v.(fmt.Formatter); ok {
e.withColor(buf, e.opts.Theme.AttrValueError(), func() {
fmt.Fprintf(buf, "%+v", v)
})
} else {
e.writeColoredString(buf, v.Error(), e.opts.Theme.AttrValueError())
}
return
case fmt.Stringer:
e.writeColoredString(buf, v.String(), attrValue)
Expand Down
15 changes: 14 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,6 +73,17 @@ func (v *theValuer) LogValue() slog.Value {
return slog.StringValue(fmt.Sprintf("The word is '%s'", v.word))
}

type formatterError struct {
error
}

func (e *formatterError) Format(f fmt.State, verb rune) {
if verb == 'v' && f.Flag('+') {
io.WriteString(f, "formatted ")
}
io.WriteString(f, e.Error())
}

func TestHandler_Attr(t *testing.T) {
buf := bytes.Buffer{}
h := NewHandler(&buf, &HandlerOptions{NoColor: true})
Expand All @@ -87,6 +99,7 @@ func TestHandler_Attr(t *testing.T) {
slog.Duration("dur", time.Second),
slog.Group("group", slog.String("foo", "bar"), slog.Group("subgroup", slog.String("foo", "bar"))),
slog.Any("err", errors.New("the error")),
slog.Any("formattedError", &formatterError{errors.New("the error")}),
slog.Any("stringer", theStringer{}),
slog.Any("nostringer", noStringer{Foo: "bar"}),
// Resolve LogValuer items in addition to Stringer items.
Expand All @@ -102,7 +115,7 @@ func TestHandler_Attr(t *testing.T) {
)
AssertNoError(t, h.Handle(context.Background(), rec))

expected := fmt.Sprintf("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar err=the error stringer=stringer nostringer={bar} valuer=The word is 'distant'\n", now.Format(time.DateTime), now.Format(time.DateTime))
expected := fmt.Sprintf("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar err=the error formattedError=formatted the error stringer=stringer nostringer={bar} valuer=The word is 'distant'\n", now.Format(time.DateTime), now.Format(time.DateTime))
AssertEqual(t, expected, buf.String())
}

Expand Down