From: Jonathan Amsterdam Date: Thu, 18 May 2023 13:24:42 +0000 (-0400) Subject: log/slog: empty calls to With and WithGroup are no-ops X-Git-Tag: go1.21rc1~380 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bc96901e8959c5eb21d5bca5614eb66481815918;p=gostls13.git log/slog: empty calls to With and WithGroup are no-ops 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 Reviewed-by: Alan Donovan Run-TryBot: Jonathan Amsterdam --- diff --git a/src/log/slog/handler.go b/src/log/slog/handler.go index cab0b5f088..b10a6bd247 100644 --- a/src/log/slog/handler.go +++ b/src/log/slog/handler.go @@ -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 diff --git a/src/log/slog/logger.go b/src/log/slog/logger.go index 6b990b35b9..2bad5dfccc 100644 --- a/src/log/slog/logger.go +++ b/src/log/slog/logger.go @@ -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")