@@ -119,6 +119,9 @@ type Options struct {
119119 // DialerRetryTimeout is the backoff duration between retry attempts.
120120 // Default: 100ms
121121 DialerRetryTimeout time.Duration
122+
123+ // Optional logger for connection pool operations.
124+ Logger internal.LoggerWithLevel
122125}
123126
124127type lastDialErrorWrap struct {
@@ -254,7 +257,7 @@ func (p *ConnPool) checkMinIdleConns() {
254257 p .idleConnsLen .Add (- 1 )
255258
256259 p .freeTurn ()
257- internal . Logger . Printf (context .Background (), "addIdleConn panic: %+v" , err )
260+ p . logger (). Errorf (context .Background (), "addIdleConn panic: %+v" , err )
258261 }
259262 }()
260263
@@ -416,7 +419,7 @@ func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) {
416419 return cn , nil
417420 }
418421
419- internal . Logger . Printf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , attempt , lastErr )
422+ p . logger (). Errorf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , attempt , lastErr )
420423 // All retries failed - handle error tracking
421424 p .setLastDialError (lastErr )
422425 if atomic .AddUint32 (& p .dialErrorsNum , 1 ) == uint32 (p .cfg .PoolSize ) {
@@ -510,10 +513,10 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
510513 acceptConn , err := hookManager .ProcessOnGet (ctx , cn , false )
511514 if err != nil || ! acceptConn {
512515 if err != nil {
513- internal . Logger . Printf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
516+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
514517 _ = p .CloseConn (cn )
515518 } else {
516- internal . Logger . Printf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
519+ p . logger (). Errorf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
517520 // Return connection to pool without freeing the turn that this Get() call holds.
518521 // We use putConnWithoutTurn() to run all the Put hooks and logic without freeing a turn.
519522 p .putConnWithoutTurn (ctx , cn )
@@ -541,7 +544,7 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
541544 // this should not happen with a new connection, but we handle it gracefully
542545 if err != nil || ! acceptConn {
543546 // Failed to process connection, discard it
544- internal . Logger . Printf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
547+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
545548 _ = p .CloseConn (newcn )
546549 return nil , err
547550 }
@@ -584,7 +587,7 @@ func (p *ConnPool) queuedNewConn(ctx context.Context) (*Conn, error) {
584587 if ! freeTurnCalled {
585588 p .freeTurn ()
586589 }
587- internal . Logger . Printf ( context . Background () , "queuedNewConn panic: %+v" , err )
590+ p . logger (). Errorf ( ctx , "queuedNewConn panic: %+v" , err )
588591 }
589592 }()
590593
@@ -728,7 +731,7 @@ func (p *ConnPool) popIdle() (*Conn, error) {
728731
729732 // If we exhausted all attempts without finding a usable connection, return nil
730733 if attempts > 1 && attempts >= maxAttempts && int32 (attempts ) >= p .poolSize .Load () {
731- internal . Logger . Printf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
734+ p . logger (). Errorf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
732735 return nil , nil
733736 }
734737
@@ -757,7 +760,7 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
757760 // Peek at the reply type to check if it's a push notification
758761 if replyType , err := cn .PeekReplyTypeSafe (); err != nil || replyType != proto .RespPush {
759762 // Not a push notification or error peeking, remove connection
760- internal . Logger . Printf (ctx , "Conn has unread data (not push notification), removing it" )
763+ p . logger (). Errorf (ctx , "Conn has unread data (not push notification), removing it" )
761764 p .removeConnInternal (ctx , cn , err , freeTurn )
762765 return
763766 }
@@ -770,7 +773,7 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
770773 if hookManager != nil {
771774 shouldPool , shouldRemove , err = hookManager .ProcessOnPut (ctx , cn )
772775 if err != nil {
773- internal . Logger . Printf (ctx , "Connection hook error: %v" , err )
776+ p . logger (). Errorf (ctx , "Connection hook error: %v" , err )
774777 p .removeConnInternal (ctx , cn , err , freeTurn )
775778 return
776779 }
@@ -803,12 +806,12 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
803806 case StateUnusable :
804807 // expected state, don't log it
805808 case StateClosed :
806- internal . Logger . Printf (ctx , "Unexpected conn[%d] state changed by hook to %v, closing it" , cn .GetID (), currentState )
809+ p . logger (). Errorf (ctx , "Unexpected conn[%d] state changed by hook to %v, closing it" , cn .GetID (), currentState )
807810 shouldCloseConn = true
808811 p .removeConnWithLock (cn )
809812 default :
810813 // Pool as-is
811- internal . Logger . Printf (ctx , "Unexpected conn[%d] state changed by hook to %v, pooling as-is" , cn .GetID (), currentState )
814+ p . logger (). Warnf (ctx , "Unexpected conn[%d] state changed by hook to %v, pooling as-is" , cn .GetID (), currentState )
812815 }
813816 }
814817
@@ -1022,7 +1025,7 @@ func (p *ConnPool) isHealthyConn(cn *Conn, nowNs int64) bool {
10221025 if replyType , err := cn .rd .PeekReplyType (); err == nil && replyType == proto .RespPush {
10231026 // For RESP3 connections with push notifications, we allow some buffered data
10241027 // The client will process these notifications before using the connection
1025- internal . Logger . Printf (
1028+ p . logger (). Infof (
10261029 context .Background (),
10271030 "push: conn[%d] has buffered data, likely push notifications - will be processed by client" ,
10281031 cn .GetID (),
@@ -1045,3 +1048,11 @@ func (p *ConnPool) isHealthyConn(cn *Conn, nowNs int64) bool {
10451048 cn .SetUsedAtNs (nowNs )
10461049 return true
10471050}
1051+
1052+ func (p * ConnPool ) logger () internal.LoggerWithLevel {
1053+ if p .cfg .Logger != nil {
1054+ return p .cfg .Logger
1055+ }
1056+
1057+ return internal .LegacyLoggerWithLevel
1058+ }
0 commit comments