Skip to content

text_formatter: add support for GoString #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chris3torek
Copy link

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