-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Milestone
Description
Proposal Details
func GroupAttrs(key, attrs....Attr) AttrI 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Type
Projects
Status
Accepted