-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add optional logger wherever possible #3560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ccoVeille
wants to merge
3
commits into
redis:master
Choose a base branch
from
ccoveille-forks:optional-logger
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package logging | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // CustomLogger is a logger interface with leveled logging methods. | ||
| // | ||
| // This interface can be implemented by custom loggers to provide leveled logging. | ||
| type CustomLogger struct { | ||
| logger LoggerWithLevel | ||
| loggerLevel *LogLevelT | ||
| printfAdapter PrintfAdapter | ||
| } | ||
|
|
||
| func NewCustomLogger(logger LoggerWithLevel, opts ...CustomLoggerOption) *CustomLogger { | ||
| cl := &CustomLogger{ | ||
| logger: logger, | ||
| } | ||
| for _, opt := range opts { | ||
| opt(cl) | ||
| } | ||
| return cl | ||
| } | ||
|
|
||
| type CustomLoggerOption func(*CustomLogger) | ||
|
|
||
| func WithPrintfAdapter(adapter PrintfAdapter) CustomLoggerOption { | ||
| return func(cl *CustomLogger) { | ||
| cl.printfAdapter = adapter | ||
| } | ||
| } | ||
|
|
||
| func WithLoggerLevel(level LogLevelT) CustomLoggerOption { | ||
| return func(cl *CustomLogger) { | ||
| cl.loggerLevel = &level | ||
| } | ||
| } | ||
|
|
||
| // PrintfAdapter is a function that converts Printf-style log messages into structured log messages. | ||
| // It can be used to extract key-value pairs from the formatted message. | ||
| type PrintfAdapter func(ctx context.Context, format string, v ...any) (context.Context, string, []any) | ||
|
|
||
| // Error is a structured error level logging method with context and arguments. | ||
| func (cl *CustomLogger) Error(ctx context.Context, msg string, args ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Errorf(ctx, msg, args...) | ||
| return | ||
| } | ||
| cl.logger.ErrorContext(ctx, msg, args...) | ||
| } | ||
|
|
||
| func (cl *CustomLogger) Errorf(ctx context.Context, format string, v ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Errorf(ctx, format, v...) | ||
| return | ||
| } | ||
| cl.logger.ErrorContext(ctx, format, v...) | ||
| } | ||
|
|
||
| // Warn is a structured warning level logging method with context and arguments. | ||
| func (cl *CustomLogger) Warn(ctx context.Context, msg string, args ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Warnf(ctx, msg, args...) | ||
| return | ||
| } | ||
| cl.logger.WarnContext(ctx, msg, args...) | ||
| } | ||
|
|
||
| func (cl *CustomLogger) Warnf(ctx context.Context, format string, v ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Warnf(ctx, format, v...) | ||
| return | ||
| } | ||
| cl.logger.WarnContext(cl.printfToStructured(ctx, format, v...)) | ||
| } | ||
|
|
||
| // Info is a structured info level logging method with context and arguments. | ||
| func (cl *CustomLogger) Info(ctx context.Context, msg string, args ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Infof(ctx, msg, args...) | ||
| return | ||
| } | ||
| cl.logger.InfoContext(ctx, msg, args...) | ||
| } | ||
|
|
||
| // Debug is a structured debug level logging method with context and arguments. | ||
| func (cl *CustomLogger) Debug(ctx context.Context, msg string, args ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Debugf(ctx, msg, args...) | ||
| return | ||
| } | ||
| cl.logger.DebugContext(ctx, msg, args...) | ||
| } | ||
|
|
||
| func (cl *CustomLogger) Infof(ctx context.Context, format string, v ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Infof(ctx, format, v...) | ||
| return | ||
| } | ||
|
|
||
| cl.logger.InfoContext(cl.printfToStructured(ctx, format, v...)) | ||
| } | ||
|
|
||
| func (cl *CustomLogger) Debugf(ctx context.Context, format string, v ...any) { | ||
| if cl == nil || cl.logger == nil { | ||
| legacyLoggerWithLevel.Debugf(ctx, format, v...) | ||
| return | ||
| } | ||
| cl.logger.DebugContext(cl.printfToStructured(ctx, format, v...)) | ||
| } | ||
|
|
||
| func (cl *CustomLogger) printfToStructured(ctx context.Context, format string, v ...any) (context.Context, string, []any) { | ||
| if cl.printfAdapter != nil { | ||
| return cl.printfAdapter(ctx, format, v...) | ||
| } | ||
| return ctx, fmt.Sprintf(format, v...), nil | ||
| } | ||
|
|
||
| func (cl *CustomLogger) Enabled(ctx context.Context, level LogLevelT) bool { | ||
| if cl.loggerLevel != nil { | ||
| return level >= *cl.loggerLevel | ||
| } | ||
|
|
||
| return legacyLoggerWithLevel.Enabled(ctx, level) | ||
| } | ||
|
|
||
| // LoggerWithLevel is a logger interface with leveled logging methods. | ||
| // | ||
| // [slog.Logger] from the standard library satisfies this interface. | ||
| type LoggerWithLevel interface { | ||
| // InfoContext logs an info level message | ||
| InfoContext(ctx context.Context, format string, v ...any) | ||
|
|
||
| // WarnContext logs a warning level message | ||
| WarnContext(ctx context.Context, format string, v ...any) | ||
|
|
||
| // Debugf logs a debug level message | ||
| DebugContext(ctx context.Context, format string, v ...any) | ||
|
|
||
| // Errorf logs an error level message | ||
| ErrorContext(ctx context.Context, format string, v ...any) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package logging | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/redis/go-redis/v9/internal" | ||
| ) | ||
|
|
||
| // legacyLoggerAdapter is a logger that implements [LoggerWithLevel] interface | ||
| // using the global [internal.Logger] and [internal.LogLevel] variables. | ||
| type legacyLoggerAdapter struct{} | ||
|
|
||
| var _ LoggerWithLevel = (*legacyLoggerAdapter)(nil) | ||
|
|
||
| // structuredToPrintf converts a structured log message and key-value pairs into something a Printf-style logger can understand. | ||
| func (l *legacyLoggerAdapter) structuredToPrintf(msg string, v ...any) (string, []any) { | ||
| format := msg | ||
| var args []any | ||
|
|
||
| for i := 0; i < len(v); i += 2 { | ||
| if i+1 >= len(v) { | ||
| break | ||
| } | ||
| format += " %v=%v" | ||
| args = append(args, v[i], v[i+1]) | ||
| } | ||
|
|
||
| return format, args | ||
| } | ||
|
|
||
| func (l legacyLoggerAdapter) Errorf(ctx context.Context, format string, v ...any) { | ||
| internal.Logger.Printf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) ErrorContext(ctx context.Context, msg string, args ...any) { | ||
| format, v := l.structuredToPrintf(msg, args...) | ||
| l.Errorf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) WarnContext(ctx context.Context, msg string, args ...any) { | ||
| format, v := l.structuredToPrintf(msg, args...) | ||
| l.Warnf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) Warnf(ctx context.Context, format string, v ...any) { | ||
| if !internal.LogLevel.WarnOrAbove() { | ||
| // Skip logging | ||
| return | ||
| } | ||
| internal.Logger.Printf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) InfoContext(ctx context.Context, msg string, args ...any) { | ||
| format, v := l.structuredToPrintf(msg, args...) | ||
| l.Infof(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) Infof(ctx context.Context, format string, v ...any) { | ||
| if !internal.LogLevel.InfoOrAbove() { | ||
| // Skip logging | ||
| return | ||
| } | ||
| internal.Logger.Printf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) DebugContext(ctx context.Context, msg string, args ...any) { | ||
| format, v := l.structuredToPrintf(msg, args...) | ||
| l.Debugf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) Debugf(ctx context.Context, format string, v ...any) { | ||
| if !internal.LogLevel.DebugOrAbove() { | ||
| // Skip logging | ||
| return | ||
| } | ||
| internal.Logger.Printf(ctx, format, v...) | ||
| } | ||
|
|
||
| func (l *legacyLoggerAdapter) Enabled(ctx context.Context, level LogLevelT) bool { | ||
| switch level { | ||
| case LogLevelDebug: | ||
| return internal.LogLevel.DebugOrAbove() | ||
| case LogLevelWarn: | ||
| return internal.LogLevel.WarnOrAbove() | ||
| case LogLevelInfo: | ||
| return internal.LogLevel.InfoOrAbove() | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| var legacyLoggerWithLevel = &legacyLoggerAdapter{} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,3 +89,4 @@ func (l *filterLogger) Printf(ctx context.Context, format string, v ...interface | |
| return | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logger may miss the last v if len(v) is not even