Skip to content

Commit 8bf1376

Browse files
committed
reorg and refactor of types
1 parent f894560 commit 8bf1376

File tree

21 files changed

+113
-115
lines changed

21 files changed

+113
-115
lines changed

commands.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/redis/go-redis/v9/internal"
16+
"github.com/redis/go-redis/v9/logging"
1617
)
1718

1819
// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
@@ -28,23 +29,15 @@ func usePrecise(dur time.Duration) bool {
2829

2930
func formatMs(ctx context.Context, dur time.Duration) int64 {
3031
if dur > 0 && dur < time.Millisecond {
31-
internal.Logger.Printf(
32-
ctx,
33-
"specified duration is %s, but minimal supported value is %s - truncating to 1ms",
34-
dur, time.Millisecond,
35-
)
32+
logging.LoggerWithLevel().Infof(ctx, "specified duration is %s, but minimal supported value is %s - truncating to 1ms", dur, time.Millisecond)
3633
return 1
3734
}
3835
return int64(dur / time.Millisecond)
3936
}
4037

4138
func formatSec(ctx context.Context, dur time.Duration) int64 {
4239
if dur > 0 && dur < time.Second {
43-
internal.Logger.Printf(
44-
ctx,
45-
"specified duration is %s, but minimal supported value is %s - truncating to 1s",
46-
dur, time.Second,
47-
)
40+
logging.LoggerWithLevel().Infof(ctx, "specified duration is %s, but minimal supported value is %s - truncating to 1s", dur, time.Second)
4841
return 1
4942
}
5043
return int64(dur / time.Second)

export_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"net"
77
"strings"
88

9-
"github.com/redis/go-redis/v9/internal"
109
"github.com/redis/go-redis/v9/internal/hashtag"
1110
"github.com/redis/go-redis/v9/internal/pool"
11+
"github.com/redis/go-redis/v9/logging"
1212
)
1313

1414
func (c *baseClient) Pool() pool.Pooler {
@@ -87,7 +87,7 @@ func (c *clusterState) IsConsistent(ctx context.Context) bool {
8787
func GetSlavesAddrByName(ctx context.Context, c *SentinelClient, name string) []string {
8888
addrs, err := c.Replicas(ctx, name).Result()
8989
if err != nil {
90-
internal.Logger.Printf(ctx, "sentinel: Replicas name=%q failed: %s",
90+
logging.LoggerWithLevel.Errorf(ctx, "sentinel: Replicas name=%q failed: %s",
9191
name, err)
9292
return []string{}
9393
}

internal/auth/streaming/pool_hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (r *ReAuthPoolHook) OnPut(_ context.Context, conn *pool.Conn) (bool, bool,
166166
defer func() {
167167
if rec := recover(); rec != nil {
168168
// once again - safety first
169-
internal.Logger.Printf(context.Background(), "panic in reauth worker: %v", rec)
169+
LoggerWrapper.Printf(context.Background(), "panic in reauth worker: %v", rec)
170170
}
171171
r.scheduledLock.Lock()
172172
delete(r.scheduledReAuth, connID)

internal/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var LogLevel LogLevelT = LogLevelError
3838
type LogLevelT int
3939

4040
// Log level constants for the entire go-redis library
41+
// TODO(ndyakov): In v10 align those levels with slog.Level
4142
const (
4243
LogLevelError LogLevelT = iota // 0 - errors only
4344
LogLevelWarn // 1 - warnings and errors

internal/pool/conn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"sync/atomic"
1212
"time"
1313

14-
"github.com/redis/go-redis/v9/internal"
1514
"github.com/redis/go-redis/v9/internal/maintnotifications/logs"
1615
"github.com/redis/go-redis/v9/internal/proto"
16+
"github.com/redis/go-redis/v9/logging"
1717
)
1818

1919
var noDeadline = time.Time{}
@@ -508,7 +508,7 @@ func (cn *Conn) getEffectiveReadTimeout(normalTimeout time.Duration) time.Durati
508508
// Deadline has passed, clear relaxed timeouts atomically and use normal timeout
509509
newCount := cn.relaxedCounter.Add(-1)
510510
if newCount <= 0 {
511-
internal.Logger.Printf(context.Background(), logs.UnrelaxedTimeoutAfterDeadline(cn.GetID()))
511+
logging.LoggerWithLevel().Infof(context.Background(), logs.UnrelaxedTimeoutAfterDeadline(cn.GetID()))
512512
cn.clearRelaxedTimeout()
513513
}
514514
return normalTimeout
@@ -542,7 +542,7 @@ func (cn *Conn) getEffectiveWriteTimeout(normalTimeout time.Duration) time.Durat
542542
// Deadline has passed, clear relaxed timeouts atomically and use normal timeout
543543
newCount := cn.relaxedCounter.Add(-1)
544544
if newCount <= 0 {
545-
internal.Logger.Printf(context.Background(), logs.UnrelaxedTimeoutAfterDeadline(cn.GetID()))
545+
logging.LoggerWithLevel().Infof(context.Background(), logs.UnrelaxedTimeoutAfterDeadline(cn.GetID()))
546546
cn.clearRelaxedTimeout()
547547
}
548548
return normalTimeout

internal/pool/pool.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ type Options struct {
122122
DialerRetryTimeout time.Duration
123123

124124
// Optional logger for connection pool operations.
125-
Logger *logging.CustomLogger
125+
Logger logging.Lgr
126126
}
127127

128128
type lastDialErrorWrap struct {
@@ -1050,10 +1050,9 @@ func (p *ConnPool) isHealthyConn(cn *Conn, nowNs int64) bool {
10501050
return true
10511051
}
10521052

1053-
func (p *ConnPool) logger() *logging.CustomLogger {
1054-
var logger *logging.CustomLogger
1053+
func (p *ConnPool) logger() logging.Lgr {
10551054
if p.cfg != nil && p.cfg.Logger != nil {
1056-
logger = p.cfg.Logger
1055+
return p.cfg.Logger
10571056
}
1058-
return logger
1057+
return logging.LoggerWithLevel()
10591058
}

logging/custom.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import (
55
"fmt"
66
)
77

8-
// CustomLogger is a logger interface with leveled logging methods.
9-
//
10-
// This interface can be implemented by custom loggers to provide leveled logging.
11-
type CustomLogger struct {
12-
logger LoggerWithLevel
8+
// LoggerWrapper is a slog.Logger wrapper that implements the Lgr interface.
9+
type LoggerWrapper struct {
10+
logger LoggerWithLevelI
1311
loggerLevel *LogLevelT
1412
printfAdapter PrintfAdapter
1513
}
1614

17-
func NewCustomLogger(logger LoggerWithLevel, opts ...CustomLoggerOption) *CustomLogger {
18-
cl := &CustomLogger{
15+
func NewLoggerWrapper(logger LoggerWithLevelI, opts ...LoggerWrapperOption) *LoggerWrapper {
16+
cl := &LoggerWrapper{
1917
logger: logger,
2018
}
2119
for _, opt := range opts {
@@ -24,16 +22,16 @@ func NewCustomLogger(logger LoggerWithLevel, opts ...CustomLoggerOption) *Custom
2422
return cl
2523
}
2624

27-
type CustomLoggerOption func(*CustomLogger)
25+
type LoggerWrapperOption func(*LoggerWrapper)
2826

29-
func WithPrintfAdapter(adapter PrintfAdapter) CustomLoggerOption {
30-
return func(cl *CustomLogger) {
27+
func WithPrintfAdapter(adapter PrintfAdapter) LoggerWrapperOption {
28+
return func(cl *LoggerWrapper) {
3129
cl.printfAdapter = adapter
3230
}
3331
}
3432

35-
func WithLoggerLevel(level LogLevelT) CustomLoggerOption {
36-
return func(cl *CustomLogger) {
33+
func WithLoggerLevel(level LogLevelT) LoggerWrapperOption {
34+
return func(cl *LoggerWrapper) {
3735
cl.loggerLevel = &level
3836
}
3937
}
@@ -43,15 +41,15 @@ func WithLoggerLevel(level LogLevelT) CustomLoggerOption {
4341
type PrintfAdapter func(ctx context.Context, format string, v ...any) (context.Context, string, []any)
4442

4543
// Error is a structured error level logging method with context and arguments.
46-
func (cl *CustomLogger) Error(ctx context.Context, msg string, args ...any) {
44+
func (cl *LoggerWrapper) Error(ctx context.Context, msg string, args ...any) {
4745
if cl == nil || cl.logger == nil {
4846
legacyLoggerWithLevel.Errorf(ctx, msg, args...)
4947
return
5048
}
5149
cl.logger.ErrorContext(ctx, msg, args...)
5250
}
5351

54-
func (cl *CustomLogger) Errorf(ctx context.Context, format string, v ...any) {
52+
func (cl *LoggerWrapper) Errorf(ctx context.Context, format string, v ...any) {
5553
if cl == nil || cl.logger == nil {
5654
legacyLoggerWithLevel.Errorf(ctx, format, v...)
5755
return
@@ -60,15 +58,15 @@ func (cl *CustomLogger) Errorf(ctx context.Context, format string, v ...any) {
6058
}
6159

6260
// Warn is a structured warning level logging method with context and arguments.
63-
func (cl *CustomLogger) Warn(ctx context.Context, msg string, args ...any) {
61+
func (cl *LoggerWrapper) Warn(ctx context.Context, msg string, args ...any) {
6462
if cl == nil || cl.logger == nil {
6563
legacyLoggerWithLevel.Warnf(ctx, msg, args...)
6664
return
6765
}
6866
cl.logger.WarnContext(ctx, msg, args...)
6967
}
7068

71-
func (cl *CustomLogger) Warnf(ctx context.Context, format string, v ...any) {
69+
func (cl *LoggerWrapper) Warnf(ctx context.Context, format string, v ...any) {
7270
if cl == nil || cl.logger == nil {
7371
legacyLoggerWithLevel.Warnf(ctx, format, v...)
7472
return
@@ -77,7 +75,7 @@ func (cl *CustomLogger) Warnf(ctx context.Context, format string, v ...any) {
7775
}
7876

7977
// Info is a structured info level logging method with context and arguments.
80-
func (cl *CustomLogger) Info(ctx context.Context, msg string, args ...any) {
78+
func (cl *LoggerWrapper) Info(ctx context.Context, msg string, args ...any) {
8179
if cl == nil || cl.logger == nil {
8280
legacyLoggerWithLevel.Infof(ctx, msg, args...)
8381
return
@@ -86,15 +84,15 @@ func (cl *CustomLogger) Info(ctx context.Context, msg string, args ...any) {
8684
}
8785

8886
// Debug is a structured debug level logging method with context and arguments.
89-
func (cl *CustomLogger) Debug(ctx context.Context, msg string, args ...any) {
87+
func (cl *LoggerWrapper) Debug(ctx context.Context, msg string, args ...any) {
9088
if cl == nil || cl.logger == nil {
9189
legacyLoggerWithLevel.Debugf(ctx, msg, args...)
9290
return
9391
}
9492
cl.logger.DebugContext(ctx, msg, args...)
9593
}
9694

97-
func (cl *CustomLogger) Infof(ctx context.Context, format string, v ...any) {
95+
func (cl *LoggerWrapper) Infof(ctx context.Context, format string, v ...any) {
9896
if cl == nil || cl.logger == nil {
9997
legacyLoggerWithLevel.Infof(ctx, format, v...)
10098
return
@@ -103,33 +101,33 @@ func (cl *CustomLogger) Infof(ctx context.Context, format string, v ...any) {
103101
cl.logger.InfoContext(cl.printfToStructured(ctx, format, v...))
104102
}
105103

106-
func (cl *CustomLogger) Debugf(ctx context.Context, format string, v ...any) {
104+
func (cl *LoggerWrapper) Debugf(ctx context.Context, format string, v ...any) {
107105
if cl == nil || cl.logger == nil {
108106
legacyLoggerWithLevel.Debugf(ctx, format, v...)
109107
return
110108
}
111109
cl.logger.DebugContext(cl.printfToStructured(ctx, format, v...))
112110
}
113111

114-
func (cl *CustomLogger) printfToStructured(ctx context.Context, format string, v ...any) (context.Context, string, []any) {
112+
func (cl *LoggerWrapper) printfToStructured(ctx context.Context, format string, v ...any) (context.Context, string, []any) {
115113
if cl != nil && cl.printfAdapter != nil {
116114
return cl.printfAdapter(ctx, format, v...)
117115
}
118116
return ctx, fmt.Sprintf(format, v...), nil
119117
}
120118

121-
func (cl *CustomLogger) Enabled(ctx context.Context, level LogLevelT) bool {
119+
func (cl *LoggerWrapper) Enabled(ctx context.Context, level LogLevelT) bool {
122120
if cl != nil && cl.loggerLevel != nil {
123121
return level >= *cl.loggerLevel
124122
}
125123

126124
return legacyLoggerWithLevel.Enabled(ctx, level)
127125
}
128126

129-
// LoggerWithLevel is a logger interface with leveled logging methods.
127+
// LoggerWithLevelI is a logger interface with leveled logging methods.
130128
//
131129
// [slog.Logger] from the standard library satisfies this interface.
132-
type LoggerWithLevel interface {
130+
type LoggerWithLevelI interface {
133131
// InfoContext logs an info level message
134132
InfoContext(ctx context.Context, format string, v ...any)
135133

logging/legacy.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"github.com/redis/go-redis/v9/internal"
77
)
88

9-
// legacyLoggerAdapter is a logger that implements [LoggerWithLevel] interface
9+
// legacyLoggerAdapter is a logger that implements [LoggerWithLevelI] interface
1010
// using the global [internal.Logger] and [internal.LogLevel] variables.
1111
type legacyLoggerAdapter struct{}
1212

13-
var _ LoggerWithLevel = (*legacyLoggerAdapter)(nil)
13+
var _ LoggerWithLevelI = (*legacyLoggerAdapter)(nil)
1414

1515
// structuredToPrintf converts a structured log message and key-value pairs into something a Printf-style logger can understand.
1616
func (l *legacyLoggerAdapter) structuredToPrintf(msg string, v ...any) (string, []any) {
@@ -92,3 +92,7 @@ func (l *legacyLoggerAdapter) Enabled(ctx context.Context, level LogLevelT) bool
9292
}
9393

9494
var legacyLoggerWithLevel = &legacyLoggerAdapter{}
95+
96+
func LoggerWithLevel() Lgr {
97+
return legacyLoggerWithLevel
98+
}

logging/logging.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ func (l *filterLogger) Printf(ctx context.Context, format string, v ...interface
9090
}
9191
}
9292

93+
// Lgr is a logger interface with leveled logging methods.
94+
// It is implemented by LoggerWrapper and legacyLoggerAdapter.
95+
// If you would like to use `slog.Logger` from the standard library, you can use LoggerWrapper.
96+
//
97+
// logger := slog.New(slog.NewTextHandler(os.Stderr))
98+
// db := redis.NewClient(&redis.Options{
99+
// Logger: logging.NewLoggerWrapper(logger),
100+
// })
101+
//
102+
// This will NOT handle all logging at the moment, singe there is still a global logger in use.
103+
// that will be deprecated in the future.
104+
type Lgr interface {
105+
Errorf(ctx context.Context, format string, v ...any)
106+
Warnf(ctx context.Context, format string, v ...any)
107+
Infof(ctx context.Context, format string, v ...any)
108+
Debugf(ctx context.Context, format string, v ...any)
109+
Enabled(ctx context.Context, level LogLevelT) bool
110+
}

maintnotifications/circuit_breaker.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,11 @@ func (cb *CircuitBreaker) GetStats() CircuitBreakerStats {
194194
}
195195
}
196196

197-
func (cb *CircuitBreaker) logger() *logging.CustomLogger {
198-
var logger *logging.CustomLogger
197+
func (cb *CircuitBreaker) logger() logging.Lgr {
199198
if cb.config != nil && cb.config.Logger != nil {
200-
logger = cb.config.Logger
199+
return cb.config.Logger
201200
}
202-
return logger
201+
return logging.LoggerWithLevel()
203202
}
204203

205204
// CircuitBreakerStats provides statistics about a circuit breaker
@@ -352,10 +351,9 @@ func (cbm *CircuitBreakerManager) Reset() {
352351
})
353352
}
354353

355-
func (cbm *CircuitBreakerManager) logger() *logging.CustomLogger {
356-
var logger *logging.CustomLogger
354+
func (cbm *CircuitBreakerManager) logger() logging.Lgr {
357355
if cbm.config != nil && cbm.config.Logger != nil {
358-
logger = cbm.config.Logger
356+
return cbm.config.Logger
359357
}
360-
return logger
358+
return logging.LoggerWithLevel()
361359
}

0 commit comments

Comments
 (0)