]> Cypherpunks repositories - gostls13.git/commitdiff
log/slog: empty calls to With and WithGroup are no-ops
authorJonathan Amsterdam <jba@google.com>
Thu, 18 May 2023 13:24:42 +0000 (09:24 -0400)
committerJonathan Amsterdam <jba@google.com>
Mon, 22 May 2023 18:27:07 +0000 (18:27 +0000)
It doesn't make sense to call Logger.WithGroup with the empty string.
Make it a no-op by returning the receiver.
This relieves handlers of the burden of detecting that case themselves.

Less importantly, but for consistency, if Logger.With is called with
no args, make it a no-op by returning the receiver.

Along the way, fix obsolete mentions of "the Logger's context" in the
doc.

Change-Id: Ia6caa4f1ca70c1c4b0cab3e222b2fda48be73fef
Reviewed-on: https://go-review.googlesource.com/c/go/+/496175
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>

src/log/slog/handler.go
src/log/slog/logger.go

index cab0b5f088452151a1c736c207c12b0e1ce60751..b10a6bd247e62923b1b442f012302e3e2ddaf34d 100644 (file)
@@ -242,9 +242,6 @@ func (h *commonHandler) withAttrs(as []Attr) *commonHandler {
 }
 
 func (h *commonHandler) withGroup(name string) *commonHandler {
-       if name == "" {
-               return h
-       }
        h2 := h.clone()
        h2.groups = append(h2.groups, name)
        return h2
index 6b990b35b9dac3f3a5a2366286e7ab5ccdb0ac94..2bad5dfccc91adef95468fdb7b4f070212bf7873 100644 (file)
@@ -88,34 +88,35 @@ func (l *Logger) clone() *Logger {
 // Handler returns l's Handler.
 func (l *Logger) Handler() Handler { return l.handler }
 
-// With returns a new Logger that includes the given arguments, converted to
-// Attrs as in [Logger.Log].
-// The Attrs will be added to each output from the Logger.
-// The new Logger shares the old Logger's context.
-// The new Logger's handler is the result of calling WithAttrs on the receiver's
-// handler.
+// With returns a Logger that includes the given attributes
+// in each output operation. Arguments are converted to
+// attributes as if by [Logger.Log].
 func (l *Logger) With(args ...any) *Logger {
+       if len(args) == 0 {
+               return l
+       }
        c := l.clone()
        c.handler = l.handler.WithAttrs(argsToAttrSlice(args))
        return c
 }
 
-// WithGroup returns a new Logger that starts a group. The keys of all
-// attributes added to the Logger will be qualified by the given name.
-// (How that qualification happens depends on the [Handler.WithGroup]
+// WithGroup returns a Logger that starts a group, if name is non-empty.
+// The keys of all attributes added to the Logger will be qualified by the given
+// name. (How that qualification happens depends on the [Handler.WithGroup]
 // method of the Logger's Handler.)
-// The new Logger shares the old Logger's context.
 //
-// The new Logger's handler is the result of calling WithGroup on the receiver's
-// handler.
+// If name is empty, WithGroup returns the receiver.
 func (l *Logger) WithGroup(name string) *Logger {
+       if name == "" {
+               return l
+       }
        c := l.clone()
        c.handler = l.handler.WithGroup(name)
        return c
 
 }
 
-// New creates a new Logger with the given non-nil Handler and a nil context.
+// New creates a new Logger with the given non-nil Handler.
 func New(h Handler) *Logger {
        if h == nil {
                panic("nil Handler")