From 2c32e3e6bfd9649e32af9d7708fba0e1f6cb7952 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Sat, 19 Jun 2021 01:01:23 +0200 Subject: [PATCH] JSONFormatter#Format(): let errors decide their JSON representation by themselves ... if they implement json.Marshaler or encoding.TextMarshaler. --- json_formatter.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/json_formatter.go b/json_formatter.go index c96dc5636..cb733afb6 100644 --- a/json_formatter.go +++ b/json_formatter.go @@ -2,6 +2,7 @@ package logrus import ( "bytes" + "encoding" "encoding/json" "fmt" "runtime" @@ -64,6 +65,10 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { data := make(Fields, len(entry.Data)+4) for k, v := range entry.Data { switch v := v.(type) { + case json.Marshaler, encoding.TextMarshaler: + // Let errors which implement either of these + // decide their JSON representation by themselves. + data[k] = v case error: // Otherwise errors are ignored by `encoding/json` // https://github.com/sirupsen/logrus/issues/137