Skip to content

Commit f54bf65

Browse files
jbaeric
authored andcommitted
log/slog: Group takes ...any
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 <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 36604ef commit f54bf65

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

api/next/56345.txt

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pkg log/slog, func Error(string, ...interface{}) #56345
4747
pkg log/slog, func ErrorCtx(context.Context, string, ...interface{}) #56345
4848
pkg log/slog, func Float64(string, float64) Attr #56345
4949
pkg log/slog, func Float64Value(float64) Value #56345
50-
pkg log/slog, func Group(string, ...Attr) Attr #56345
5150
pkg log/slog, func GroupValue(...Attr) Value #56345
5251
pkg log/slog, func Info(string, ...interface{}) #56345
5352
pkg log/slog, func InfoCtx(context.Context, string, ...interface{}) #56345

api/next/59204.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg log/slog, func Group(string, ...interface{}) Attr #59204

src/log/slog/attr.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,26 @@ func Duration(key string, v time.Duration) Attr {
5858
}
5959

6060
// Group returns an Attr for a Group Value.
61-
// The caller must not subsequently mutate the
62-
// argument slice.
61+
// The first argument is the key; the remaining arguments
62+
// are converted to Attrs as in [Logger.Log].
6363
//
64-
// Use Group to collect several Attrs under a single
64+
// Use Group to collect several key-value pairs under a single
6565
// key on a log line, or as the result of LogValue
6666
// in order to log a single value as multiple Attrs.
67-
func Group(key string, as ...Attr) Attr {
68-
return Attr{key, GroupValue(as...)}
67+
func Group(key string, args ...any) Attr {
68+
return Attr{key, GroupValue(argsToAttrSlice(args)...)}
69+
}
70+
71+
func argsToAttrSlice(args []any) []Attr {
72+
var (
73+
attr Attr
74+
attrs []Attr
75+
)
76+
for len(args) > 0 {
77+
attr, args = argsToAttr(args)
78+
attrs = append(attrs, attr)
79+
}
80+
return attrs
6981
}
7082

7183
// Any returns an Attr for the supplied value.

src/log/slog/doc.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ How this qualification is displayed depends on the handler.
164164
[TextHandler] separates the group and attribute names with a dot.
165165
[JSONHandler] treats each group as a separate JSON object, with the group name as the key.
166166
167-
Use [Group] to create a Group Attr from a name and a list of Attrs:
167+
Use [Group] to create a Group Attr from a name and a list of key-value pairs:
168168
169169
slog.Group("request",
170-
slog.String("method", r.Method),
171-
slog.Any("url", r.URL))
170+
"method", r.Method,
171+
"url", r.URL)
172172
173173
TextHandler would display this group as
174174

src/log/slog/logger.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,8 @@ func (l *Logger) Handler() Handler { return l.handler }
9595
// The new Logger's handler is the result of calling WithAttrs on the receiver's
9696
// handler.
9797
func (l *Logger) With(args ...any) *Logger {
98-
var (
99-
attr Attr
100-
attrs []Attr
101-
)
102-
for len(args) > 0 {
103-
attr, args = argsToAttr(args)
104-
attrs = append(attrs, attr)
105-
}
10698
c := l.clone()
107-
c.handler = l.handler.WithAttrs(attrs)
99+
c.handler = l.handler.WithAttrs(argsToAttrSlice(args))
108100
return c
109101
}
110102

0 commit comments

Comments
 (0)