Skip to content
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: GroupAttrs(key, attrs....[]Attr) Attr #66365

Open
seankhliao opened this issue Mar 17, 2024 · 0 comments
Open

proposal: log/slog: GroupAttrs(key, attrs....[]Attr) Attr #66365

seankhliao opened this issue Mar 17, 2024 · 0 comments
Labels
Milestone

Comments

@seankhliao
Copy link
Member

Proposal Details

func GroupAttrs(key, attrs....[]Attr) Attr

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:

// Works
g := slog.Group("key",
        slog.String("key-1", "a"),
        slog.Int("key-2", 2),
)

but less well if you need to construct the group members beforehand.

// maybe constructed
attrs := []slog.Attr{
        slog.String("key-1", "a"),
}
if x {
        attrs = append(attrs, slog.Int("key-2", 2))
}

// Doesn't work, []slog.Attr doesn't match []any
g := slog.Group("key", attrs...)

Existing code generally appears to use one of the 3 options from below,
in ascending order of popularity.

// Option 1: Unintuitive group construction
g := slog.Any("key", slog.GroupValue(attrs...))

// Option 2: convert to []any with a helper func
g := slog.Group("key", attr2any(attrs)...)

// Option 3: just use []any
var attrs []any
...
g := slog.Group("key", attrs...)

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 keeping slog.Handler) because Attrs are how key-value pairs are communicated to handlers.

cc @jba

@gopherbot gopherbot added this to the Proposal milestone Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

2 participants