diff --git a/README.md b/README.md index b7a116a..90d62b4 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ opts := &devslog.Options{ TimeFormat: "[04:05]", NewLineAfterLog: true, DebugColor: devslog.Magenta, + StringerFormatter: true, } logger := slog.New(devslog.NewHandler(os.Stdout, opts)) @@ -61,6 +62,7 @@ opts := &devslog.Options{ MaxSlicePrintSize: 4, SortKeys: true, NewLineAfterLog: true, + StringerFormatter: true, } logger := slog.New(devslog.NewHandler(os.Stdout, opts)) @@ -85,6 +87,7 @@ if production { MaxSlicePrintSize: 10, SortKeys: true, NewLineAfterLog: true, + StringerFormatter: true, } logger = slog.New(devslog.NewHandler(os.Stdout, opts)) @@ -107,3 +110,4 @@ slog.SetDefault(logger) | WarnColor | Color for Warn level | devslog.Yellow | devslog.Color (uint) | | ErrorColor | Color for Error level | devslog.Red | devslog.Color (uint) | | MaxErrorStackTrace | Max stack trace frames for errors | 0 | uint | +| StringerFormatter | Use Stringer interface for formatting | false | bool | diff --git a/devslog.go b/devslog.go index 1c2910b..e1185dd 100644 --- a/devslog.go +++ b/devslog.go @@ -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 { @@ -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 { @@ -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) diff --git a/devslog_test.go b/devslog_test.go index a164fb6..1aa87fe 100644 --- a/devslog_test.go +++ b/devslog_test.go @@ -57,6 +57,7 @@ func Test_Types(t *testing.T) { SortKeys: true, TimeFormat: "[]", NewLineAfterLog: true, + StringerFormatter: true, } test_String(t, opts) @@ -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) { @@ -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) + } +}