log/slog: document context.Background use in non-Context methods

Document that Debug, Info, Warn, Error (both Logger methods and
package-level functions) use context.Background internally, and
point users to their *Context variants.

Updates #44143

Change-Id: I1bfb7bc1b0d050dfa6a665233f3df9ee70510153
Reviewed-on: https://go-review.googlesource.com/c/go/+/752620
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
This commit is contained in:
kovan 2026-03-07 10:12:25 +01:00 committed by Gopher Robot
parent 4e06ed21ac
commit 9df04115d6

View file

@ -195,6 +195,8 @@ func (l *Logger) LogAttrs(ctx context.Context, level Level, msg string, attrs ..
}
// Debug logs at [LevelDebug].
// It uses [context.Background] internally; to specify the context, use
// [Logger.DebugContext].
func (l *Logger) Debug(msg string, args ...any) {
l.log(context.Background(), LevelDebug, msg, args...)
}
@ -205,6 +207,8 @@ func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any) {
}
// Info logs at [LevelInfo].
// It uses [context.Background] internally; to specify the context, use
// [Logger.InfoContext].
func (l *Logger) Info(msg string, args ...any) {
l.log(context.Background(), LevelInfo, msg, args...)
}
@ -215,6 +219,8 @@ func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any) {
}
// Warn logs at [LevelWarn].
// It uses [context.Background] internally; to specify the context, use
// [Logger.WarnContext].
func (l *Logger) Warn(msg string, args ...any) {
l.log(context.Background(), LevelWarn, msg, args...)
}
@ -225,6 +231,8 @@ func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any) {
}
// Error logs at [LevelError].
// It uses [context.Background] internally; to specify the context, use
// [Logger.ErrorContext].
func (l *Logger) Error(msg string, args ...any) {
l.log(context.Background(), LevelError, msg, args...)
}
@ -277,6 +285,8 @@ func (l *Logger) logAttrs(ctx context.Context, level Level, msg string, attrs ..
}
// Debug calls [Logger.Debug] on the default logger.
// It uses [context.Background] internally; to specify the context, use
// [DebugContext].
func Debug(msg string, args ...any) {
Default().log(context.Background(), LevelDebug, msg, args...)
}
@ -287,6 +297,8 @@ func DebugContext(ctx context.Context, msg string, args ...any) {
}
// Info calls [Logger.Info] on the default logger.
// It uses [context.Background] internally; to specify the context, use
// [InfoContext].
func Info(msg string, args ...any) {
Default().log(context.Background(), LevelInfo, msg, args...)
}
@ -297,6 +309,8 @@ func InfoContext(ctx context.Context, msg string, args ...any) {
}
// Warn calls [Logger.Warn] on the default logger.
// It uses [context.Background] internally; to specify the context, use
// [WarnContext].
func Warn(msg string, args ...any) {
Default().log(context.Background(), LevelWarn, msg, args...)
}
@ -307,6 +321,8 @@ func WarnContext(ctx context.Context, msg string, args ...any) {
}
// Error calls [Logger.Error] on the default logger.
// It uses [context.Background] internally; to specify the context, use
// [ErrorContext].
func Error(msg string, args ...any) {
Default().log(context.Background(), LevelError, msg, args...)
}