mirror of https://github.com/golang/go.git
log/slog: log/logAttrs after single ctx initialization
In extreme cases (ctx = nil), it is recommended to initialize the context only once at the entry point of the log/logAttrs.
This commit is contained in:
parent
1763ee199d
commit
e1719b9539
|
|
@ -238,6 +238,9 @@ func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) {
|
||||||
// It must always be called directly by an exported logging method
|
// It must always be called directly by an exported logging method
|
||||||
// or function, because it uses a fixed call depth to obtain the pc.
|
// or function, because it uses a fixed call depth to obtain the pc.
|
||||||
func (l *Logger) log(ctx context.Context, level Level, msg string, args ...any) {
|
func (l *Logger) log(ctx context.Context, level Level, msg string, args ...any) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
if !l.Enabled(ctx, level) {
|
if !l.Enabled(ctx, level) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -250,14 +253,14 @@ func (l *Logger) log(ctx context.Context, level Level, msg string, args ...any)
|
||||||
}
|
}
|
||||||
r := NewRecord(time.Now(), level, msg, pc)
|
r := NewRecord(time.Now(), level, msg, pc)
|
||||||
r.Add(args...)
|
r.Add(args...)
|
||||||
if ctx == nil {
|
|
||||||
ctx = context.Background()
|
|
||||||
}
|
|
||||||
_ = l.Handler().Handle(ctx, r)
|
_ = l.Handler().Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// logAttrs is like [Logger.log], but for methods that take ...Attr.
|
// logAttrs is like [Logger.log], but for methods that take ...Attr.
|
||||||
func (l *Logger) logAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
|
func (l *Logger) logAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
if !l.Enabled(ctx, level) {
|
if !l.Enabled(ctx, level) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -270,9 +273,6 @@ func (l *Logger) logAttrs(ctx context.Context, level Level, msg string, attrs ..
|
||||||
}
|
}
|
||||||
r := NewRecord(time.Now(), level, msg, pc)
|
r := NewRecord(time.Now(), level, msg, pc)
|
||||||
r.AddAttrs(attrs...)
|
r.AddAttrs(attrs...)
|
||||||
if ctx == nil {
|
|
||||||
ctx = context.Background()
|
|
||||||
}
|
|
||||||
_ = l.Handler().Handle(ctx, r)
|
_ = l.Handler().Handle(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue