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
--- /dev/null
+pkg log/slog, func Group(string, ...interface{}) Attr #59204
}
// 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.
[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
// 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
}