diff --git a/handler.go b/handler.go index 2cdaf7c..092799b 100644 --- a/handler.go +++ b/handler.go @@ -145,7 +145,11 @@ func processAttr(attr slog.Attr, target map[string]any) { case attr.Key == "": // skip default: - target[attr.Key] = rv.Any() + if rvAsError, isError := rv.Any().(error); isError { + target[attr.Key] = rvAsError.Error() + } else { + target[attr.Key] = rv.Any() + } } } diff --git a/handler_test.go b/handler_test.go index 8d087bc..3cd6433 100644 --- a/handler_test.go +++ b/handler_test.go @@ -8,6 +8,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gstruct" + "fmt" "log/slog" "strconv" "strings" @@ -59,6 +60,20 @@ var _ = Describe("NewHandler", func() { }))) }) + It("logs an error message", func() { + slog.New(h).Error("foo", "error", fmt.Errorf("boom")) + logs := s.Logs() + Expect(logs).To(ConsistOf(MatchFields(IgnoreExtras, Fields{ + "Source": Equal("test"), + "Message": Equal("test.foo"), + "Data": SatisfyAll( + HaveLen(1), + HaveKeyWithValue("error", "boom"), + ), + "LogLevel": Equal(lager.ERROR), + }))) + }) + It("behaves like a slog.NewHandler", func() { results := func() (result []map[string]any) { for _, l := range s.Logs() {