From d6a8ba41c44943eada59dbba873aa3618c0d36ec Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 10 Sep 2022 09:08:20 -0400 Subject: [PATCH] slog: implement argsToAttrs directly Now that we have argsToAttr (singular), we can easily construct a slice of Attr without going through Record. Change-Id: I3175316fef1997a6063140fcfe499c174e6d3071 Reviewed-on: https://go-review.googlesource.com/c/exp/+/430095 Run-TryBot: Jonathan Amsterdam Reviewed-by: Alan Donovan --- slog/logger.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/slog/logger.go b/slog/logger.go index e653e27dd..52b8f2c84 100644 --- a/slog/logger.go +++ b/slog/logger.go @@ -66,22 +66,24 @@ func (l *Logger) Handler() Handler { return l.handler } // With returns a new Logger whose handler's attributes are a concatenation of // l's attributes and the given arguments, converted to Attrs as in // [Logger.Log]. -func (l *Logger) With(attrs ...any) *Logger { - return &Logger{handler: l.handler.With(argsToAttrs(attrs))} -} - -func argsToAttrs(args []any) []Attr { - var r Record - setAttrs(&r, args) - return r.Attrs() +func (l *Logger) With(args ...any) *Logger { + var ( + attr Attr + attrs []Attr + ) + for len(args) > 0 { + attr, args = argsToAttr(args) + attrs = append(attrs, attr) + } + return &Logger{handler: l.handler.With(attrs)} } // New creates a new Logger with the given Handler. func New(h Handler) *Logger { return &Logger{handler: h} } // With calls Logger.With on the default logger. -func With(attrs ...any) *Logger { - return Default().With(attrs...) +func With(args ...any) *Logger { + return Default().With(args...) } // Enabled reports whether l emits log records at the given level. @@ -138,7 +140,7 @@ func setAttrs(r *Record, args []any) { } } -// argsToAttrs turns a prefix of the args slice into an Attr and returns +// argsToAttr turns a prefix of the args slice into an Attr and returns // the unused portion of the slice. // If args[0] is an Attr, it returns it. // If args[0] is a string, it treats the first two elements as