-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
log/slog: change slog.Group signature to ...any #59204
Comments
I don't have a strong opinion on this but I agree there is some value in having the standard ("suggested", "conventional"?) conversion defined and easily available. Another situation where a common conversion might be useful is when implementing alternatives to Rather than changing For groups, something like this would result in more tokens than changing the signature to slog.Group("label", slog.Attrs("a", 1, "b", 2)...) |
CC @jba |
I'm generally in favor of this; I agree there should be some way to expose the logic that converts k-v pairs to Attrs. The Linking to the main proposal: #56345. |
It's worth noting that you can easily do Attrs in terms of Group anyway, |
It sounds like people are in favor of slog.Group taking ...any over adding slog.Attrs. Do I have that right? |
This proposal has been added to the active column of the proposals project |
Suppose someone wants to write an alternative to |
Not sure if this is what you are asking, but you can still build a group using |
I don't think that's what I'm asking. At the moment none of the Attr constructors accept I understand the desire to provide a tool for people who want access to the the |
That is an interesting point. But wouldn't they just go the whole way?
Yes, there are a lot of those shims, but it would probably take no more than an hour and since slog is in the standard library, your maintenance burden would be small—a few tweaks every six months. And then you'd be completely locked down: no chance of calling |
Now that you've show us how, I suppose they would. That approach had not occurred to me yet, though, so it might not occur to others. ;). But it's good to know it's possible and enterprising individuals can find the path. Thanks. |
Is there a use case for |
I am not sure. If there is a use case for code to create variables of type But when I review code that has variables like that with our internal logging package it usually seems like a code smell and should be replaced by a use of |
I would characterize the utility as syntactic, in a language that on principle doesn't embrace syntax metaprogramming ... there's some tension in this and it seems like a sensitive question. A dumb playground example of what I mean: it's possible to prepare any error with file:line annotation and arbitrary additional logging attributes while still passing it around as a valid
I did some quick benchmarking of a type Attr = slog.Attr
func groupAny(name string, args ...any) Attr {
as := make([]Attr, 0, len(args))
var a Attr
for len(args) > 0 {
a, args = argsToAttr(args)
as = append(as, a)
}
return Attr{name, slog.GroupValue(as...)}
}
func AttrsFunc(args ...any) []Attr {
as := make([]Attr, 0, len(args))
var a Attr
for len(args) > 0 {
a, args = argsToAttr(args)
as = append(as, a)
}
return as
} |
Here is another consideration, briefly mentioned in the top post:
To elaborate Ian's argument: we've decided that
and jarring to require that arguments to Indeed, a developer who only needed to produce log output, and didn't care about how Handlers or LogValuers worked, wouldn't even have to learn about Attrs and Values except for To me, this is a strong reason to prefer changing the signature of |
Have all remaining concerns about this proposal been addressed? |
Based on the discussion above, this proposal seems like a likely accept. |
Change https://go.dev/cl/487855 mentions this issue: |
No change in consensus, so accepted. 🎉 |
The Group function takes a key and a ...any, which is converted into attrs. Fixes golang#59204. Change-Id: Ib714365dcda2eda37863ce433f3dd8cf5eeda610 Reviewed-on: https://go-review.googlesource.com/c/go/+/487855 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
The Group function takes a key and a ...any, which is converted into attrs. Fixes golang#59204. Change-Id: Ib714365dcda2eda37863ce433f3dd8cf5eeda610 Reviewed-on: https://go-review.googlesource.com/c/go/+/487855 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Currently there is now way to access the standard conversion of
...any
to...Attr
without building a record.It would be nice to be able to build a group
Attr
this way, for patterns where you want to be able to pass around a set of attrs (like queries that return a standard set for logging, or building a set of labels before an if that adds more).All existing calls to
slog.Group
would keep working as the ...any forms happily accept Attr directly anyway, they would just be a fraction slower, in cases where the performance is needed you could use GroupValue directly.This also exposes the standard conversion to helpers that are not performance critical so they can have a signature that looks like the normal logging functions.
Performance critical helpers need to build a Record directly to avoid allocations, and could not use this approach, but exposing an API for that is a much larger surface area.
The text was updated successfully, but these errors were encountered: