]> Cypherpunks.ru repositories - gostls13.git/commitdiff
log/slog: Group takes ...any
authorJonathan Amsterdam <jba@google.com>
Wed, 19 Apr 2023 18:56:58 +0000 (14:56 -0400)
committerJonathan Amsterdam <jba@google.com>
Thu, 4 May 2023 19:53:09 +0000 (19:53 +0000)
The Group function takes a key and a ...any, which is converted
into attrs.

Fixes #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>

api/next/56345.txt
api/next/59204.txt [new file with mode: 0644]
src/log/slog/attr.go
src/log/slog/doc.go
src/log/slog/logger.go

index c11ce6871e6bbfcb9e964ca51b4da9168680b60c..fe96bddc865f164ea99d26c438bbcee7b538a2e7 100644 (file)
@@ -47,7 +47,6 @@ pkg log/slog, func Error(string, ...interface{}) #56345
 pkg log/slog, func ErrorCtx(context.Context, string, ...interface{}) #56345
 pkg log/slog, func Float64(string, float64) Attr #56345
 pkg log/slog, func Float64Value(float64) Value #56345
-pkg log/slog, func Group(string, ...Attr) Attr #56345
 pkg log/slog, func GroupValue(...Attr) Value #56345
 pkg log/slog, func Info(string, ...interface{}) #56345
 pkg log/slog, func InfoCtx(context.Context, string, ...interface{}) #56345
diff --git a/api/next/59204.txt b/api/next/59204.txt
new file mode 100644 (file)
index 0000000..e39e410
--- /dev/null
@@ -0,0 +1 @@
+pkg log/slog, func Group(string, ...interface{}) Attr #59204
index cd3bacca43bf720824807861dd9b4b9a392b5127..a180d0e1d3d0f8a9dfeeec0e97af4dd6af3bf493 100644 (file)
@@ -58,14 +58,26 @@ func Duration(key string, v time.Duration) Attr {
 }
 
 // Group returns an Attr for a Group Value.
-// The caller must not subsequently mutate the
-// argument slice.
+// The first argument is the key; the remaining arguments
+// are converted to Attrs as in [Logger.Log].
 //
-// Use Group to collect several Attrs under a single
+// Use Group to collect several key-value pairs under a single
 // key on a log line, or as the result of LogValue
 // in order to log a single value as multiple Attrs.
-func Group(key string, as ...Attr) Attr {
-       return Attr{key, GroupValue(as...)}
+func Group(key string, args ...any) Attr {
+       return Attr{key, GroupValue(argsToAttrSlice(args)...)}
+}
+
+func argsToAttrSlice(args []any) []Attr {
+       var (
+               attr  Attr
+               attrs []Attr
+       )
+       for len(args) > 0 {
+               attr, args = argsToAttr(args)
+               attrs = append(attrs, attr)
+       }
+       return attrs
 }
 
 // Any returns an Attr for the supplied value.
index 205c40de90607fdade25d4e01026f89e22779ef9..57f83bb52dc4195a2383ef34b49cfdfe4873e321 100644 (file)
@@ -164,11 +164,11 @@ How this qualification is displayed depends on the handler.
 [TextHandler] separates the group and attribute names with a dot.
 [JSONHandler] treats each group as a separate JSON object, with the group name as the key.
 
-Use [Group] to create a Group Attr from a name and a list of Attrs:
+Use [Group] to create a Group Attr from a name and a list of key-value pairs:
 
        slog.Group("request",
-           slog.String("method", r.Method),
-           slog.Any("url", r.URL))
+           "method", r.Method,
+           "url", r.URL)
 
 TextHandler would display this group as
 
index c997dd31dc679023a640976464d32e6c9f4ab9ea..6b990b35b9dac3f3a5a2366286e7ab5ccdb0ac94 100644 (file)
@@ -95,16 +95,8 @@ func (l *Logger) Handler() Handler { return l.handler }
 // The new Logger's handler is the result of calling WithAttrs on the receiver's
 // handler.
 func (l *Logger) With(args ...any) *Logger {
-       var (
-               attr  Attr
-               attrs []Attr
-       )
-       for len(args) > 0 {
-               attr, args = argsToAttr(args)
-               attrs = append(attrs, attr)
-       }
        c := l.clone()
-       c.handler = l.handler.WithAttrs(attrs)
+       c.handler = l.handler.WithAttrs(argsToAttrSlice(args))
        return c
 }