@@ -115,6 +115,9 @@ type Options struct {
115115 // DialerRetryTimeout is the backoff duration between retry attempts.
116116 // Default: 100ms
117117 DialerRetryTimeout time.Duration
118+
119+ // Optional logger for connection pool operations.
120+ Logger internal.LoggerWithLevel
118121}
119122
120123type lastDialErrorWrap struct {
@@ -223,7 +226,7 @@ func (p *ConnPool) checkMinIdleConns() {
223226 p .idleConnsLen .Add (- 1 )
224227
225228 p .freeTurn ()
226- internal . Logger . Printf (context .Background (), "addIdleConn panic: %+v" , err )
229+ p . logger (). Errorf (context .Background (), "addIdleConn panic: %+v" , err )
227230 }
228231 }()
229232
@@ -379,7 +382,7 @@ func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) {
379382 return cn , nil
380383 }
381384
382- internal . Logger . Printf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , maxRetries , lastErr )
385+ p . logger (). Errorf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , maxRetries , lastErr )
383386 // All retries failed - handle error tracking
384387 p .setLastDialError (lastErr )
385388 if atomic .AddUint32 (& p .dialErrorsNum , 1 ) == uint32 (p .cfg .PoolSize ) {
@@ -452,7 +455,7 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
452455
453456 for {
454457 if attempts >= getAttempts {
455- internal . Logger . Printf (ctx , "redis: connection pool: was not able to get a healthy connection after %d attempts" , attempts )
458+ p . logger (). Errorf (ctx , "redis: connection pool: was not able to get a healthy connection after %d attempts" , attempts )
456459 break
457460 }
458461 attempts ++
@@ -479,12 +482,12 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
479482 if hookManager != nil {
480483 acceptConn , err := hookManager .ProcessOnGet (ctx , cn , false )
481484 if err != nil {
482- internal . Logger . Printf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
485+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
483486 _ = p .CloseConn (cn )
484487 continue
485488 }
486489 if ! acceptConn {
487- internal . Logger . Printf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
490+ p . logger (). Errorf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
488491 p .Put (ctx , cn )
489492 cn = nil
490493 continue
@@ -509,7 +512,7 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
509512 // this should not happen with a new connection, but we handle it gracefully
510513 if err != nil || ! acceptConn {
511514 // Failed to process connection, discard it
512- internal . Logger . Printf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
515+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
513516 _ = p .CloseConn (newcn )
514517 return nil , err
515518 }
@@ -703,7 +706,7 @@ func (p *ConnPool) popIdle() (*Conn, error) {
703706
704707 // If we exhausted all attempts without finding a usable connection, return nil
705708 if attempts > 1 && attempts >= maxAttempts && int32 (attempts ) >= p .poolSize .Load () {
706- internal . Logger . Printf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
709+ p . logger (). Errorf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
707710 return nil , nil
708711 }
709712
@@ -720,7 +723,7 @@ func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
720723 // Peek at the reply type to check if it's a push notification
721724 if replyType , err := cn .PeekReplyTypeSafe (); err != nil || replyType != proto .RespPush {
722725 // Not a push notification or error peeking, remove connection
723- internal . Logger . Printf (ctx , "Conn has unread data (not push notification), removing it" )
726+ p . logger (). Errorf (ctx , "Conn has unread data (not push notification), removing it" )
724727 p .Remove (ctx , cn , err )
725728 }
726729 // It's a push notification, allow pooling (client will handle it)
@@ -733,7 +736,7 @@ func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
733736 if hookManager != nil {
734737 shouldPool , shouldRemove , err = hookManager .ProcessOnPut (ctx , cn )
735738 if err != nil {
736- internal . Logger . Printf (ctx , "Connection hook error: %v" , err )
739+ p . logger (). Errorf (ctx , "Connection hook error: %v" , err )
737740 p .Remove (ctx , cn , err )
738741 return
739742 }
@@ -835,7 +838,7 @@ func (p *ConnPool) removeConn(cn *Conn) {
835838 // this can be idle conn
836839 for idx , ic := range p .idleConns {
837840 if ic .GetID () == cid {
838- internal . Logger . Printf (context .Background (), "redis: connection pool: removing idle conn[%d]" , cid )
841+ p . logger (). Errorf (context .Background (), "redis: connection pool: removing idle conn[%d]" , cid )
839842 p .idleConns = append (p .idleConns [:idx ], p .idleConns [idx + 1 :]... )
840843 p .idleConnsLen .Add (- 1 )
841844 break
@@ -951,7 +954,7 @@ func (p *ConnPool) isHealthyConn(cn *Conn, now time.Time) bool {
951954 if replyType , err := cn .rd .PeekReplyType (); err == nil && replyType == proto .RespPush {
952955 // For RESP3 connections with push notifications, we allow some buffered data
953956 // The client will process these notifications before using the connection
954- internal . Logger . Printf (context .Background (), "push: conn[%d] has buffered data, likely push notifications - will be processed by client" , cn .GetID ())
957+ p . logger (). Infof (context .Background (), "push: conn[%d] has buffered data, likely push notifications - will be processed by client" , cn .GetID ())
955958 return true // Connection is healthy, client will handle notifications
956959 }
957960 return false // Unexpected data, not push notifications, connection is unhealthy
@@ -961,3 +964,11 @@ func (p *ConnPool) isHealthyConn(cn *Conn, now time.Time) bool {
961964 }
962965 return true
963966}
967+
968+ func (p * ConnPool ) logger () internal.LoggerWithLevel {
969+ if p .cfg .Logger != nil {
970+ return p .cfg .Logger
971+ }
972+
973+ return internal .LegacyLoggerWithLevel
974+ }
0 commit comments