From c9dc6d765844da5c599137ae76103ab2f154b722 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 14 Nov 2023 20:25:46 +0100 Subject: [PATCH] log: fix terminal format --- log/format.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/log/format.go b/log/format.go index 1be0723045..cdb884ce9d 100644 --- a/log/format.go +++ b/log/format.go @@ -124,10 +124,19 @@ func (h *TerminalHandler) logfmt(buf *bytes.Buffer, r slog.Record, color int) { } // formatValue formats a value for serialization -func FormatLogfmtValue(value interface{}, term bool) string { +func FormatLogfmtValue(value interface{}, term bool) (result string) { if value == nil { return "nil" } + defer func() { + if err := recover(); err != nil { + if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr && v.IsNil() { + result = "nil" + } else { + panic(err) + } + } + }() switch v := value.(type) { case time.Time: @@ -151,11 +160,6 @@ func FormatLogfmtValue(value interface{}, term bool) string { return "nil" } return formatLogfmtUint256(v) - case error: - return v.Error() - - case fmt.Stringer: - return v.String() } if term { if s, ok := value.(TerminalStringer); ok { @@ -164,6 +168,10 @@ func FormatLogfmtValue(value interface{}, term bool) string { } } switch v := value.(type) { + case error: + return v.Error() + case fmt.Stringer: + return v.String() case bool: return strconv.FormatBool(v) case float32: