aboutsummaryrefslogtreecommitdiff
path: root/src/log
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2023-05-18 09:24:42 -0400
committerJonathan Amsterdam <jba@google.com>2023-05-22 18:27:07 +0000
commitbc96901e8959c5eb21d5bca5614eb66481815918 (patch)
tree6897ccd2017d7ac4bf3f994a6979301911de45e6 /src/log
parent6d4d71c5abe104d95ede3aa2f3eaaef7bc613ebb (diff)
downloadgo-bc96901e8959c5eb21d5bca5614eb66481815918.tar.gz
go-bc96901e8959c5eb21d5bca5614eb66481815918.zip
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 <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'src/log')
-rw-r--r--src/log/slog/handler.go3
-rw-r--r--src/log/slog/logger.go27
2 files changed, 14 insertions, 16 deletions
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")