From 35cbc9c46ed0bc337ad8842e88515185bbd96d86 Mon Sep 17 00:00:00 2001 From: joeriddles Date: Mon, 21 Apr 2025 20:37:56 -0600 Subject: [PATCH 1/4] Add failing test for slog.Any error with JSON --- handler_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/handler_test.go b/handler_test.go index cc26864..e98c20d 100644 --- a/handler_test.go +++ b/handler_test.go @@ -3,6 +3,7 @@ package shandler_test import ( "bytes" "context" + "errors" "fmt" "io" "log/slog" @@ -249,6 +250,18 @@ func TestErrorTags(t *testing.T) { assert.Contains(t, stderr.String(), fmt.Sprintf("[ERROR] %s - test error_id=", now)) } +func TestSlogAnyErrorWithJsonOption(t *testing.T) { + var stderr bytes.Buffer + now := time.Now().Format(time.TimeOnly) + logger := slog.New(handler.NewHandler( + handler.WithStdErr(&stderr), + handler.WithJSON(), + )) + err := errors.New("big error") + logger.Error("test", slog.Any("err", err)) + assert.Equal(t, fmt.Sprintf(`{"level":"ERROR","time":"%s","message":"test","attrs":{"err":"big error"}}\n`, now), stderr.String()) +} + func BenchmarkHandlers(b *testing.B) { var stdout bytes.Buffer bt := []struct { From c3d0f10e4cc4dc5ceadd7e8f920988f6ed86708f Mon Sep 17 00:00:00 2001 From: joeriddles Date: Mon, 21 Apr 2025 20:54:28 -0600 Subject: [PATCH 2/4] Test text and json --- handler_test.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/handler_test.go b/handler_test.go index e98c20d..65565d4 100644 --- a/handler_test.go +++ b/handler_test.go @@ -250,16 +250,35 @@ func TestErrorTags(t *testing.T) { assert.Contains(t, stderr.String(), fmt.Sprintf("[ERROR] %s - test error_id=", now)) } -func TestSlogAnyErrorWithJsonOption(t *testing.T) { - var stderr bytes.Buffer +func TestSlogAnyError(t *testing.T) { now := time.Now().Format(time.TimeOnly) - logger := slog.New(handler.NewHandler( - handler.WithStdErr(&stderr), - handler.WithJSON(), - )) - err := errors.New("big error") - logger.Error("test", slog.Any("err", err)) - assert.Equal(t, fmt.Sprintf(`{"level":"ERROR","time":"%s","message":"test","attrs":{"err":"big error"}}\n`, now), stderr.String()) + tests := []struct { + name string + opts []handler.HandlerOption + expected string + }{ + { + name: "text", + opts: []handler.HandlerOption{}, + expected: fmt.Sprintf(`[ERROR] %s - test err=big error`, now), + }, + { + name: "json", + opts: []handler.HandlerOption{handler.WithJSON()}, + expected: fmt.Sprintf(`{"level":"ERROR","time":"%s","message":"test","attrs":{"err":"big error"}}`, now), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var stderr bytes.Buffer + tt.opts = append(tt.opts, handler.WithStdErr(&stderr)) + logger := slog.New(handler.NewHandler(tt.opts...)) + err := errors.New("big error") + logger.Error("test", slog.Any("err", err)) + actual := strings.TrimRight(stderr.String(), "\n") + assert.Equal(t, tt.expected, actual) + }) + } } func BenchmarkHandlers(b *testing.B) { From 38fa7237d4fa17c94bcb4fb5aeff71dd46a57f28 Mon Sep 17 00:00:00 2001 From: joeriddles Date: Mon, 21 Apr 2025 20:54:35 -0600 Subject: [PATCH 3/4] Make json test pass --- handler.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/handler.go b/handler.go index af76de7..6b7dfa6 100644 --- a/handler.go +++ b/handler.go @@ -403,3 +403,30 @@ type jsonLog struct { Attrs map[string]any `json:"attrs,omitempty"` Pid string `json:"pid,omitempty"` } + +func (j jsonLog) MarshalJSON() ([]byte, error) { + a := map[string]any{} + for key, value := range j.Attrs { + if err, ok := value.(error); ok { + a[key] = err.Error() + } else { + a[key] = value + } + } + + return json.Marshal(struct { + Level string `json:"level"` + Time string `json:"time"` + Message string `json:"message"` + Group string `json:"group,omitempty"` + Attrs map[string]any `json:"attrs,omitempty"` + Pid string `json:"pid,omitempty"` + }{ + Level: j.Level, + Time: j.Time, + Message: j.Message, + Group: j.Group, + Attrs: a, + Pid: j.Pid, + }) +} From d0d4905fc3776a75e26c4ad2fc7e0b8085e15d89 Mon Sep 17 00:00:00 2001 From: joeriddles Date: Mon, 21 Apr 2025 20:56:30 -0600 Subject: [PATCH 4/4] Simplify jsonLog.MarshalJSON --- handler.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/handler.go b/handler.go index 6b7dfa6..39635f2 100644 --- a/handler.go +++ b/handler.go @@ -414,14 +414,7 @@ func (j jsonLog) MarshalJSON() ([]byte, error) { } } - return json.Marshal(struct { - Level string `json:"level"` - Time string `json:"time"` - Message string `json:"message"` - Group string `json:"group,omitempty"` - Attrs map[string]any `json:"attrs,omitempty"` - Pid string `json:"pid,omitempty"` - }{ + return json.Marshal(jsonLog{ Level: j.Level, Time: j.Time, Message: j.Message,