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.
This commit is contained in:
Chris Torek 2020-01-04 02:50:53 -08:00
parent 67a7fdcf74
commit d87f80b7a0
1 changed files with 11 additions and 0 deletions

View File

@ -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)