Skip to content

text_formatter: add support for GoString#1087

Open
chris3torek wants to merge 1 commit into
sirupsen:masterfrom
chris3torek:gostring
Open

text_formatter: add support for GoString#1087
chris3torek wants to merge 1 commit into
sirupsen:masterfrom
chris3torek:gostring

Conversation

@chris3torek

Copy link
Copy Markdown

Note: I have not created an issue for this and don't really feel all that strongly one way or another about it. I was experimenting with some logging options and it seemed to me that logging fields in their GoString output format was clearer when they weren't quoted.

Without this, given a type like this (the Stringer and GoStringer are the same mainly for illustration):

type Walrus struct {
        name   string
        weight float64
}

func (w Walrus) GoString() string {
        return fmt.Sprintf("Walrus{name: %q, weight: %g}", w.name, w.weight)
}
func (w Walrus) String() string {
        return fmt.Sprintf("Walrus{name: %q, weight: %g}", w.name, w.weight)
}
...
        log.WithField("walrus", Walrus{"Fred", 300}).Debug("walrus details")

produces (I've trimmed a bit of interior whitespace for this PR text):

DEBU[0000] walrus details                walrus="Walrus{name: \"Fred\", weight: 300}"

With GoString enabled we get:

DEBU[0000] walrus details                walrus=Walrus{name: "Fred", weight: 300}

which just seems prettier. A well-formed GoString should produce unambiguously encoded results, so it won't need quotes.

Commit message text follows...


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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants