From d87f80b7a0e3d5510459135be907ee987d20a195 Mon Sep 17 00:00:00 2001 From: Chris Torek Date: Sat, 4 Jan 2020 02:50:53 -0800 Subject: [PATCH] text_formatter: add support for GoString Allow logging fields with their GoString result. When using GoString, never add extra quotes, regardless of the ForceQuote setting: the GoString format should take care of this. Note that if some field does not implement the GoString interface, we fall through to the remaining logic, which obeys ForceQuote as usual. --- text_formatter.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/text_formatter.go b/text_formatter.go index 5867b25..0dcc76f 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -34,6 +34,10 @@ type TextFormatter struct { // Force disabling colors. DisableColors bool + // try GoString() for field representation: if that works, + // don't quote the field at all, just print the result. + UseGoString bool + // Force quoting of all values ForceQuote bool @@ -313,6 +317,13 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf } func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { + if f.UseGoString { + stringer, ok := value.(fmt.GoStringer) + if ok { + b.WriteString(stringer.GoString()) + return + } + } stringVal, ok := value.(string) if !ok { stringVal = fmt.Sprint(value)