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
proposal: log/slog: make it easier to log types #63570
Comments
Argument against: It's "just a string" |
CC @jba |
I don't think logging a type is common. Zap has 77 constructors for a |
Using my (slightly old) downloaded copy of https://github.com/mvdan/corpus, some very quick and dirty stats:
That's 2% as many A quick random sample, by piping rg into perl and dropping almost all lines:
Of note, there are multiple instances of |
If we add this, would we ever regret the possible ambiguity of |
Maybe? But we collectively have lots of code miles on %T. |
Which is an oversight, IMO. I usually end up writing a constructor for |
I don't currently understand why slog.Warn("unexpected type", "x", slog.TypeValue(x)) would be better than the already possible slog.Warn("unexpected type", "x", reflect.TypeOf(x)) |
@ianthehat oh, that's nice. that seems like an excellent approach. funny, i asked a bunch of experienced gophers for how to do this and you're the first to suggest it. :) Proposal withdrawn. (Maybe there's something left to do here around docs or linters...but I'm not sure what/where.) |
Translating logging including the
%T
format verb to slog is verbose. The clearest, most concise way also adds fmt overhead.I propose that we add API to make it nicer and cheaper. One possibility:
This is (in my opinion) lot clearer and simpler, and definitely cheaper.
In package slog:
There could (should?) be a matching
Type
func that returns anAttr
.I am fairly new to log/slog, so there may be some other API design that better fits the package. I don't feel strongly about any of the details.
This is, strictly speaking, all covered by the core string support, since we are just translating types to their names, which are strings. But getting the name of a type for logging is such a common need that it has a dedicated fmt verb. I would argue that the core logging package should make it similarly clear, simple, and cheap.
cc @jba
The text was updated successfully, but these errors were encountered: