Skip to content

Commit d6a8ba4

Browse files
committed
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 <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 82e28ad commit d6a8ba4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

slog/logger.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,24 @@ func (l *Logger) Handler() Handler { return l.handler }
6666
// With returns a new Logger whose handler's attributes are a concatenation of
6767
// l's attributes and the given arguments, converted to Attrs as in
6868
// [Logger.Log].
69-
func (l *Logger) With(attrs ...any) *Logger {
70-
return &Logger{handler: l.handler.With(argsToAttrs(attrs))}
71-
}
72-
73-
func argsToAttrs(args []any) []Attr {
74-
var r Record
75-
setAttrs(&r, args)
76-
return r.Attrs()
69+
func (l *Logger) With(args ...any) *Logger {
70+
var (
71+
attr Attr
72+
attrs []Attr
73+
)
74+
for len(args) > 0 {
75+
attr, args = argsToAttr(args)
76+
attrs = append(attrs, attr)
77+
}
78+
return &Logger{handler: l.handler.With(attrs)}
7779
}
7880

7981
// New creates a new Logger with the given Handler.
8082
func New(h Handler) *Logger { return &Logger{handler: h} }
8183

8284
// With calls Logger.With on the default logger.
83-
func With(attrs ...any) *Logger {
84-
return Default().With(attrs...)
85+
func With(args ...any) *Logger {
86+
return Default().With(args...)
8587
}
8688

8789
// Enabled reports whether l emits log records at the given level.
@@ -138,7 +140,7 @@ func setAttrs(r *Record, args []any) {
138140
}
139141
}
140142

141-
// argsToAttrs turns a prefix of the args slice into an Attr and returns
143+
// argsToAttr turns a prefix of the args slice into an Attr and returns
142144
// the unused portion of the slice.
143145
// If args[0] is an Attr, it returns it.
144146
// If args[0] is a string, it treats the first two elements as

0 commit comments

Comments
 (0)