Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stringer support. Fix #29 #34

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -61,6 +62,7 @@ opts := &devslog.Options{
MaxSlicePrintSize: 4,
SortKeys: true,
NewLineAfterLog: true,
StringerFormatter: true,
}

logger := slog.New(devslog.NewHandler(os.Stdout, opts))
Expand All @@ -85,6 +87,7 @@ if production {
MaxSlicePrintSize: 10,
SortKeys: true,
NewLineAfterLog: true,
StringerFormatter: true,
}

logger = slog.New(devslog.NewHandler(os.Stdout, opts))
Expand All @@ -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 |
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)
}
}
Loading