@@ -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 {
@@ -257,7 +260,7 @@ func (p *ConnPool) checkMinIdleConns() {
257260 p .idleConnsLen .Add (- 1 )
258261
259262 p .freeTurn ()
260- internal . Logger . Printf (context .Background (), "addIdleConn panic: %+v" , err )
263+ p . logger (). Errorf (context .Background (), "addIdleConn panic: %+v" , err )
261264 }
262265 }()
263266
@@ -419,7 +422,7 @@ func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) {
419422 return cn , nil
420423 }
421424
422- internal . Logger . Printf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , attempt , lastErr )
425+ p . logger (). Errorf (ctx , "redis: connection pool: failed to dial after %d attempts: %v" , attempt , lastErr )
423426 // All retries failed - handle error tracking
424427 p .setLastDialError (lastErr )
425428 if atomic .AddUint32 (& p .dialErrorsNum , 1 ) == uint32 (p .cfg .PoolSize ) {
@@ -513,10 +516,10 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
513516 acceptConn , err := hookManager .ProcessOnGet (ctx , cn , false )
514517 if err != nil || ! acceptConn {
515518 if err != nil {
516- internal . Logger . Printf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
519+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process idle connection by hook: %v" , err )
517520 _ = p .CloseConn (cn )
518521 } else {
519- internal . Logger . Printf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
522+ p . logger (). Errorf (ctx , "redis: connection pool: conn[%d] rejected by hook, returning to pool" , cn .GetID ())
520523 // Return connection to pool without freeing the turn that this Get() call holds.
521524 // We use putConnWithoutTurn() to run all the Put hooks and logic without freeing a turn.
522525 p .putConnWithoutTurn (ctx , cn )
@@ -544,7 +547,7 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) {
544547 // this should not happen with a new connection, but we handle it gracefully
545548 if err != nil || ! acceptConn {
546549 // Failed to process connection, discard it
547- internal . Logger . Printf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
550+ p . logger (). Errorf (ctx , "redis: connection pool: failed to process new connection conn[%d] by hook: accept=%v, err=%v" , newcn .GetID (), acceptConn , err )
548551 _ = p .CloseConn (newcn )
549552 return nil , err
550553 }
@@ -587,7 +590,7 @@ func (p *ConnPool) queuedNewConn(ctx context.Context) (*Conn, error) {
587590 if ! freeTurnCalled {
588591 p .freeTurn ()
589592 }
590- internal . Logger . Printf ( context . Background () , "queuedNewConn panic: %+v" , err )
593+ p . logger (). Errorf ( ctx , "queuedNewConn panic: %+v" , err )
591594 }
592595 }()
593596
@@ -731,7 +734,7 @@ func (p *ConnPool) popIdle() (*Conn, error) {
731734
732735 // If we exhausted all attempts without finding a usable connection, return nil
733736 if attempts > 1 && attempts >= maxAttempts && int32 (attempts ) >= p .poolSize .Load () {
734- internal . Logger . Printf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
737+ p . logger (). Errorf (context .Background (), "redis: connection pool: failed to get a usable connection after %d attempts" , attempts )
735738 return nil , nil
736739 }
737740
@@ -760,7 +763,7 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
760763 // Peek at the reply type to check if it's a push notification
761764 if replyType , err := cn .PeekReplyTypeSafe (); err != nil || replyType != proto .RespPush {
762765 // Not a push notification or error peeking, remove connection
763- internal . Logger . Printf (ctx , "Conn has unread data (not push notification), removing it" )
766+ p . logger (). Errorf (ctx , "Conn has unread data (not push notification), removing it" )
764767 p .removeConnInternal (ctx , cn , err , freeTurn )
765768 return
766769 }
@@ -773,7 +776,7 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
773776 if hookManager != nil {
774777 shouldPool , shouldRemove , err = hookManager .ProcessOnPut (ctx , cn )
775778 if err != nil {
776- internal . Logger . Printf (ctx , "Connection hook error: %v" , err )
779+ p . logger (). Errorf (ctx , "Connection hook error: %v" , err )
777780 p .removeConnInternal (ctx , cn , err , freeTurn )
778781 return
779782 }
@@ -806,12 +809,12 @@ func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) {
806809 case StateUnusable :
807810 // expected state, don't log it
808811 case StateClosed :
809- internal . Logger . Printf (ctx , "Unexpected conn[%d] state changed by hook to %v, closing it" , cn .GetID (), currentState )
812+ p . logger (). Errorf (ctx , "Unexpected conn[%d] state changed by hook to %v, closing it" , cn .GetID (), currentState )
810813 shouldCloseConn = true
811814 p .removeConnWithLock (cn )
812815 default :
813816 // Pool as-is
814- internal . Logger . Printf (ctx , "Unexpected conn[%d] state changed by hook to %v, pooling as-is" , cn .GetID (), currentState )
817+ p . logger (). Warnf (ctx , "Unexpected conn[%d] state changed by hook to %v, pooling as-is" , cn .GetID (), currentState )
815818 }
816819 }
817820
@@ -1028,7 +1031,7 @@ func (p *ConnPool) isHealthyConn(cn *Conn, nowNs int64) bool {
10281031 if replyType , err := cn .rd .PeekReplyType (); err == nil && replyType == proto .RespPush {
10291032 // For RESP3 connections with push notifications, we allow some buffered data
10301033 // The client will process these notifications before using the connection
1031- internal . Logger . Printf (
1034+ p . logger (). Infof (
10321035 context .Background (),
10331036 "push: conn[%d] has buffered data, likely push notifications - will be processed by client" ,
10341037 cn .GetID (),
@@ -1051,3 +1054,11 @@ func (p *ConnPool) isHealthyConn(cn *Conn, nowNs int64) bool {
10511054 cn .SetUsedAtNs (nowNs )
10521055 return true
10531056}
1057+
1058+ func (p * ConnPool ) logger () internal.LoggerWithLevel {
1059+ if p .cfg .Logger != nil {
1060+ return p .cfg .Logger
1061+ }
1062+
1063+ return internal .LegacyLoggerWithLevel
1064+ }
0 commit comments