Skip to content

Commit

Permalink
add fmt.Stringer support
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostiam authored and david-littlefarmer committed Jun 26, 2024
1 parent 4299e8a commit c890902
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
16 changes: 16 additions & 0 deletions devslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Options struct {

// Max stack trace frames when unwrapping errors
MaxErrorStackTrace uint

// Use method String() for formatting value
StringerFormatter bool
}

type groupOrAttrs struct {
Expand Down Expand Up @@ -311,6 +314,13 @@ func (h *developHandler) colorize(b []byte, as attributes, l int, g []string) []
break
}

if h.opts.StringerFormatter {
if stringer, ok := av.(fmt.Stringer); ok {
v = []byte(stringer.String())
break
}
}

avt := reflect.TypeOf(av)
avv := reflect.ValueOf(av)
if avt == nil {
Expand Down Expand Up @@ -547,6 +557,12 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i
return atb(v)
}

if h.opts.StringerFormatter {
if stringer, ok := v.Interface().(fmt.Stringer); ok {
return []byte(stringer.String())
}
}

switch v.Kind() {
case reflect.Array:
b = h.formatSlice(t, v, l+1)
Expand Down
53 changes: 53 additions & 0 deletions devslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Test_Types(t *testing.T) {
SortKeys: true,
TimeFormat: "[]",
NewLineAfterLog: true,
StringerFormatter: true,
}

test_String(t, opts)
Expand All @@ -73,6 +74,8 @@ func Test_Types(t *testing.T) {
test_Group(t, opts)
test_LogValuer(t, opts)
test_LogValuerPanic(t, opts)
test_Stringer(t, opts)
test_StringerInner(t, opts)
}

func test_NewHandlerDefaults(t *testing.T) {
Expand Down Expand Up @@ -808,3 +811,53 @@ func test_LogValuerPanic(t *testing.T, o *Options) {
t.Errorf("\nGot:\n%s\n , %[1]q expected it to contain panic stack trace", w.WrittenData)
}
}

type logStringerExample1 struct {
A []byte
}

func (item logStringerExample1) String() string {
return fmt.Sprintf("A: %s", item.A)
}

func test_Stringer(t *testing.T, o *Options) {
w := &MockWriter{}
logger := slog.New(NewHandler(w, o))
item1 := logStringerExample1{
A: []byte("test"),
}
logger.Info("test_stringer",
slog.Any("item1", item1),
)

expected := []byte("\x1b[2m\x1b[37m[]\x1b[0m \x1b[42m\x1b[30m INFO \x1b[0m \x1b[32mtest_stringer\x1b[0m\n \x1b[35mitem1\x1b[0m: A: test\n\n")

if !bytes.Equal(w.WrittenData, expected) {
t.Errorf("\nExpected:\n%s\nGot:\n%s\nExpected:\n%[1]q\nGot:\n%[2]q", expected, w.WrittenData)
}
}

type logStringerExample2 struct {
Inner logStringerExample1
Other int
}

func test_StringerInner(t *testing.T, o *Options) {
w := &MockWriter{}
logger := slog.New(NewHandler(w, o))
item1 := logStringerExample2{
Inner: logStringerExample1{
A: []byte("test"),
},
Other: 42,
}
logger.Info("test_stringer_inner",
slog.Any("item1", item1),
)

expected := []byte("\x1b[2m\x1b[37m[]\x1b[0m \x1b[42m\x1b[30m INFO \x1b[0m \x1b[32mtest_stringer_inner\x1b[0m\n\x1b[33mS\x1b[0m \x1b[35mitem1\x1b[0m: \x1b[33md\x1b[0m\x1b[33me\x1b[0m\x1b[33mv\x1b[0m\x1b[33ms\x1b[0m\x1b[33ml\x1b[0m\x1b[33mo\x1b[0m\x1b[33mg\x1b[0m\x1b[33m.\x1b[0m\x1b[33ml\x1b[0m\x1b[33mo\x1b[0m\x1b[33mg\x1b[0m\x1b[33mS\x1b[0m\x1b[33mt\x1b[0m\x1b[33mr\x1b[0m\x1b[33mi\x1b[0m\x1b[33mn\x1b[0m\x1b[33mg\x1b[0m\x1b[33me\x1b[0m\x1b[33mr\x1b[0m\x1b[33mE\x1b[0m\x1b[33mx\x1b[0m\x1b[33ma\x1b[0m\x1b[33mm\x1b[0m\x1b[33mp\x1b[0m\x1b[33ml\x1b[0m\x1b[33me\x1b[0m\x1b[33m2\x1b[0m\n \x1b[32mInner\x1b[0m: A: test\n \x1b[32mOther\x1b[0m: \x1b[33m42\x1b[0m\n\n")

if !bytes.Equal(w.WrittenData, expected) {
t.Errorf("\nExpected:\n%s\nGot:\n%s\nExpected:\n%[1]q\nGot:\n%[2]q", expected, w.WrittenData)
}
}

0 comments on commit c890902

Please sign in to comment.