Skip to content

Commit

Permalink
slog: rename XXXCtx to XXXContext, deprecate old names
Browse files Browse the repository at this point in the history
See golang/go#61200 for, um, context.

Add XXXContext methods, but keep the XXXCtx methods around
(deprecated) to avoid breaking people.

Change-Id: I6835658615fb4979b03f5ae0516ac8f79b0f241d
Reviewed-on: https://go-review.googlesource.com/c/exp/+/508215
Run-TryBot: Jonathan Amsterdam <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
  • Loading branch information
jba committed Jul 11, 2023
1 parent fffb143 commit 06a737e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
4 changes: 2 additions & 2 deletions slog/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ argument, as do their corresponding top-level functions.
Although the convenience methods on Logger (Info and so on) and the
corresponding top-level functions do not take a context, the alternatives ending
in "Ctx" do. For example,
in "Context" do. For example,
slog.InfoCtx(ctx, "message")
slog.InfoContext(ctx, "message")
It is recommended to pass a context to an output method if one is available.
Expand Down
62 changes: 55 additions & 7 deletions slog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ func (l *Logger) Debug(msg string, args ...any) {
l.log(nil, LevelDebug, msg, args...)
}

// DebugContext logs at LevelDebug with the given context.
func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelDebug, msg, args...)
}

// DebugCtx logs at LevelDebug with the given context.
// Deprecated: Use Logger.DebugContext.
func (l *Logger) DebugCtx(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelDebug, msg, args...)
}
Expand All @@ -177,7 +183,13 @@ func (l *Logger) Info(msg string, args ...any) {
l.log(nil, LevelInfo, msg, args...)
}

// InfoContext logs at LevelInfo with the given context.
func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelInfo, msg, args...)
}

// InfoCtx logs at LevelInfo with the given context.
// Deprecated: Use Logger.InfoContext.
func (l *Logger) InfoCtx(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelInfo, msg, args...)
}
Expand All @@ -187,7 +199,13 @@ func (l *Logger) Warn(msg string, args ...any) {
l.log(nil, LevelWarn, msg, args...)
}

// WarnContext logs at LevelWarn with the given context.
func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelWarn, msg, args...)
}

// WarnCtx logs at LevelWarn with the given context.
// Deprecated: Use Logger.WarnContext.
func (l *Logger) WarnCtx(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelWarn, msg, args...)
}
Expand All @@ -197,7 +215,13 @@ func (l *Logger) Error(msg string, args ...any) {
l.log(nil, LevelError, msg, args...)
}

// ErrorContext logs at LevelError with the given context.
func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelError, msg, args...)
}

// ErrorCtx logs at LevelError with the given context.
// Deprecated: Use Logger.ErrorContext.
func (l *Logger) ErrorCtx(ctx context.Context, msg string, args ...any) {
l.log(ctx, LevelError, msg, args...)
}
Expand Down Expand Up @@ -249,8 +273,8 @@ func Debug(msg string, args ...any) {
Default().log(nil, LevelDebug, msg, args...)
}

// DebugCtx calls Logger.DebugCtx on the default logger.
func DebugCtx(ctx context.Context, msg string, args ...any) {
// DebugContext calls Logger.DebugContext on the default logger.
func DebugContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelDebug, msg, args...)
}

Expand All @@ -259,8 +283,8 @@ func Info(msg string, args ...any) {
Default().log(nil, LevelInfo, msg, args...)
}

// InfoCtx calls Logger.InfoCtx on the default logger.
func InfoCtx(ctx context.Context, msg string, args ...any) {
// InfoContext calls Logger.InfoContext on the default logger.
func InfoContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelInfo, msg, args...)
}

Expand All @@ -269,8 +293,8 @@ func Warn(msg string, args ...any) {
Default().log(nil, LevelWarn, msg, args...)
}

// WarnCtx calls Logger.WarnCtx on the default logger.
func WarnCtx(ctx context.Context, msg string, args ...any) {
// WarnContext calls Logger.WarnContext on the default logger.
func WarnContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelWarn, msg, args...)
}

Expand All @@ -279,7 +303,31 @@ func Error(msg string, args ...any) {
Default().log(nil, LevelError, msg, args...)
}

// ErrorCtx calls Logger.ErrorCtx on the default logger.
// ErrorContext calls Logger.ErrorContext on the default logger.
func ErrorContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelError, msg, args...)
}

// DebugCtx calls Logger.DebugContext on the default logger.
// Deprecated: call DebugContext.
func DebugCtx(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelDebug, msg, args...)
}

// InfoCtx calls Logger.InfoContext on the default logger.
// Deprecated: call InfoContext.
func InfoCtx(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelInfo, msg, args...)
}

// WarnCtx calls Logger.WarnContext on the default logger.
// Deprecated: call WarnContext.
func WarnCtx(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelWarn, msg, args...)
}

// ErrorCtx calls Logger.ErrorContext on the default logger.
// Deprecated: call ErrorContext.
func ErrorCtx(ctx context.Context, msg string, args ...any) {
Default().log(ctx, LevelError, msg, args...)
}
Expand Down

0 comments on commit 06a737e

Please sign in to comment.