[MediaWiki-commits] [Gerrit] RedisConnectionPool: convert to PSR3 logging - change (mediawiki/core)
jenkins-bot has submitted this change and it was merged. Change subject: RedisConnectionPool: convert to PSR3 logging .. RedisConnectionPool: convert to PSR3 logging Convert from wfDebug* logging to Psr\Log\LoggerInterface. Use structured logging data to tag log messages the associated redis server. Bug: T88649 Change-Id: I5fc4c68e52b13a688bdcc93d9defc9f973323241 --- M includes/clientpool/RedisConnectionPool.php 1 file changed, 87 insertions(+), 15 deletions(-) Approvals: Aaron Schulz: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/clientpool/RedisConnectionPool.php b/includes/clientpool/RedisConnectionPool.php index dc95727..0d00d11 100644 --- a/includes/clientpool/RedisConnectionPool.php +++ b/includes/clientpool/RedisConnectionPool.php @@ -22,6 +22,10 @@ * @author Aaron Schulz */ +use MediaWiki\Logger\LoggerFactory; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; + /** * Helper class to manage Redis connections. * @@ -69,6 +73,11 @@ const SERVER_DOWN_TTL = 30; /** +* @var LoggerInterface +*/ + protected $logger; + + /** * @param array $options * @throws MWException */ @@ -76,6 +85,11 @@ if ( !class_exists( 'Redis' ) ) { throw new MWException( __CLASS__ . ' requires a Redis client library. ' . 'See https://www.mediawiki.org/wiki/Redis#Setup' ); + } + if ( isset( $options['logger'] ) ) { + $this->setLogger( $options['logger'] ); + } else { + $this->setLogger( LoggerFactory::getInstance( 'redis' ) ); } $this->connectTimeout = $options['connectTimeout']; $this->readTimeout = $options['readTimeout']; @@ -90,6 +104,14 @@ } else { throw new MWException( "Invalid serializer specified." ); } + } + + /** +* @param LoggerInterface $logger +* @return null +*/ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; } /** @@ -136,7 +158,9 @@ // Initialize the object at the hash as needed... if ( !isset( self::$instances[$id] ) ) { self::$instances[$id] = new self( $options ); - wfDebug( "Creating a new " . __CLASS__ . " instance with id $id.\n" ); + LoggerFactory::getInstance( 'redis' )->debug( + "Creating a new " . __CLASS__ . " instance with id $id." + ); } return self::$instances[$id]; @@ -161,8 +185,11 @@ unset( $this->downServers[$server] ); } else { // Server is dead - wfDebug( "server $server is marked down for another " . - ( $this->downServers[$server] - $now ) . " seconds, can't get connection\n" ); + $this->logger->debug( + 'Server "{redis_server}" is marked down for another ' . + ( $this->downServers[$server] - $now ) . 'seconds', + array( 'redis_server' => $server ) + ); return false; } @@ -175,7 +202,9 @@ $connection['free'] = false; --$this->idlePoolSize; - return new RedisConnRef( $this, $server, $connection['conn'] ); + return new RedisConnRef( + $this, $server, $connection['conn'], $this->logger + ); } } } @@ -206,7 +235,10 @@ $result = $conn->connect( $host, $port, $this->connectTimeout ); } if ( !$result ) { - wfDebugLog( 'redis', "Could not connect to server $server" ); + $this->logger->error( + 'Could not connect to server "{redis_server}"', + array( 'redis_server' => $server ) + ); // Mark server down for some time to avoid further timeouts $this->downServers[$server] = time() + self::SERVER_DOWN_TTL; @@ -214,12 +246,21 @@
[MediaWiki-commits] [Gerrit] RedisConnectionPool: convert to PSR3 logging - change (mediawiki/core)
BryanDavis has uploaded a new change for review. https://gerrit.wikimedia.org/r/227873 Change subject: RedisConnectionPool: convert to PSR3 logging .. RedisConnectionPool: convert to PSR3 logging Convert from wfDebug* logging to Psr\Log\LoggerInterface. Use structured logging data to tag log messages the associated redis server. Bug: T88649 Change-Id: I5fc4c68e52b13a688bdcc93d9defc9f973323241 --- M includes/clientpool/RedisConnectionPool.php 1 file changed, 86 insertions(+), 15 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/73/227873/1 diff --git a/includes/clientpool/RedisConnectionPool.php b/includes/clientpool/RedisConnectionPool.php index dc95727..d932e24 100644 --- a/includes/clientpool/RedisConnectionPool.php +++ b/includes/clientpool/RedisConnectionPool.php @@ -22,6 +22,10 @@ * @author Aaron Schulz */ +use MediaWiki\Logger\LoggerFactory; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; + /** * Helper class to manage Redis connections. * @@ -69,6 +73,11 @@ const SERVER_DOWN_TTL = 30; /** +* @var LoggerInterface +*/ + protected $logger; + + /** * @param array $options * @throws MWException */ @@ -76,6 +85,11 @@ if ( !class_exists( 'Redis' ) ) { throw new MWException( __CLASS__ . ' requires a Redis client library. ' . 'See https://www.mediawiki.org/wiki/Redis#Setup' ); + } + if ( isset( $options['logger'] ) ) { + $this->setLogger( $options['logger'] ); + } else { + $this->setLogger( LoggerFactory::getInstance( 'redis' ) ); } $this->connectTimeout = $options['connectTimeout']; $this->readTimeout = $options['readTimeout']; @@ -90,6 +104,14 @@ } else { throw new MWException( "Invalid serializer specified." ); } + } + + /** +* @param LoggerInterface $logger +* @return null +*/ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; } /** @@ -136,7 +158,9 @@ // Initialize the object at the hash as needed... if ( !isset( self::$instances[$id] ) ) { self::$instances[$id] = new self( $options ); - wfDebug( "Creating a new " . __CLASS__ . " instance with id $id.\n" ); + LoggerFactory::getInstance( 'redis' )->debug( + "Creating a new " . __CLASS__ . " instance with id $id." + ); } return self::$instances[$id]; @@ -161,8 +185,11 @@ unset( $this->downServers[$server] ); } else { // Server is dead - wfDebug( "server $server is marked down for another " . - ( $this->downServers[$server] - $now ) . " seconds, can't get connection\n" ); + $this->logger->debug( + 'server "{redis_server}" is marked down for another ' . + ( $this->downServers[$server] - $now ) . 'seconds', + array( 'redis_server' => $server ) + ); return false; } @@ -175,7 +202,9 @@ $connection['free'] = false; --$this->idlePoolSize; - return new RedisConnRef( $this, $server, $connection['conn'] ); + return new RedisConnRef( + $this, $server, $connection['conn'], $this->logger + ); } } } @@ -206,7 +235,10 @@ $result = $conn->connect( $host, $port, $this->connectTimeout ); } if ( !$result ) { - wfDebugLog( 'redis', "Could not connect to server $server" ); + $this->logger->error( + 'Could not connect to server "{redis_server}"', + array( 'redis_server' => $server ) + ); // Mark server down for some time to avoid further timeouts $this->downServers[$server] = time() + self::SERVER_DOWN_TTL; @@