-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: log/slog: GroupAttrs(key, attrs....Attr) Attr #66365
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
Comments
What's wrong with Yes, there's an What if we added doc |
I think it's just unintuitive and difficult to type / not helped by any sort of autocomplete? There are almost as many instances of attrs2any (19): https://github.com/search?q=language%3Ago+%2Ffunc+.*%5C%5B%5C%5Dslog%5C.Attr%5C%29+%5C%5B%5C%5Dany%2F&type=code |
Thanks for the data. Of the 46 files in your search, 21 could benefit from GroupAttrs. It's clear that people are working around the lack of this function. I'm in favor. /cc @aclements |
This proposal has been added to the active column of the proposals project |
Why wouldn't this work? attrs := []any{
slog.String("key-1", "a"),
}
if x {
attrs = append(attrs, slog.Int("key-2", 2))
}
g := slog.Group("key", attrs...) |
@willfaught That does work. The point is that you have to use |
This seems pretty reasonable, but I'd like to see the proposed doc comment for |
I think we can copy the LogAttrs docs and say: // GroupAttrs is a more efficient version of [Group] that accepts only [Attr]s
func GroupAttrs(key, attrs ...Attr) Attr |
can always use slog.Attr{Key: ..., Value: ...}.
Not arguing against the proposal. |
@kaey has a good point. Just to make it more crisp, option 4 is: // Option 4
g := slog.Attr{Key: key, Value: slog.GroupValue(attrs...)} This is type-safe, avoids But maybe this is too obscure (the fact that it took a month for someone to point it out suggests that it is 😀 ) and is worth wrapping in a helper function anyway. |
Proposal Details
I propose the addition of the above function to allow for the type safe construction of Group attributes.
The existing signature works if you know the list of attributes to use upfront:
but less well if you need to construct the group members beforehand.
Existing code generally appears to use one of the 3 options from below,
in ascending order of popularity.
This appears to be the only gap in slog's exposed api where
any
is unavoidable,and can't be escaped by using an alternate frontend (replace
slog.Logger
with something else while keepingslog.Handler
) because Attrs are how key-value pairs are communicated to handlers.cc @jba
The text was updated successfully, but these errors were encountered: