*/
$wgObjectCaches = array(
CACHE_NONE => array( 'class' => 'EmptyBagOStuff' ),
- CACHE_DB => array( 'class' => 'SqlBagOStuff' ),
+ CACHE_DB => array( 'class' => 'SqlBagOStuff', 'loggroup' => 'SQLBagOStuff' ),
CACHE_ANYTHING => array( 'factory' => 'ObjectCache::newAnything' ),
CACHE_ACCEL => array( 'factory' => 'ObjectCache::newAccelerator' ),
- CACHE_MEMCACHED => array( 'factory' => 'ObjectCache::newMemcached' ),
+ CACHE_MEMCACHED => array( 'factory' => 'ObjectCache::newMemcached', 'loggroup' => 'memcached' ),
'apc' => array( 'class' => 'APCBagOStuff' ),
'xcache' => array( 'class' => 'XCacheBagOStuff' ),
'wincache' => array( 'class' => 'WinCacheBagOStuff' ),
- 'memcached-php' => array( 'class' => 'MemcachedPhpBagOStuff' ),
- 'memcached-pecl' => array( 'class' => 'MemcachedPeclBagOStuff' ),
+ 'memcached-php' => array( 'class' => 'MemcachedPhpBagOStuff', 'loggroup' => 'memcached' ),
+ 'memcached-pecl' => array( 'class' => 'MemcachedPeclBagOStuff', 'loggroup' => 'memcached' ),
'hash' => array( 'class' => 'HashBagOStuff' ),
);
* @defgroup Cache Cache
*/
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
/**
* interface is intended to be more or less compatible with
* the PHP memcached client.
*
* @ingroup Cache
*/
-abstract class BagOStuff {
+abstract class BagOStuff implements LoggerAwareInterface {
private $debugMode = false;
protected $lastError = self::ERR_NONE;
+ /**
+ * @var LoggerInterface
+ */
+ protected $logger;
+
/** Possible values for getLastError() */
const ERR_NONE = 0; // no error
const ERR_NO_RESPONSE = 1; // no response
const ERR_UNREACHABLE = 2; // can't connect
const ERR_UNEXPECTED = 3; // response gave some error
+ public function __construct( array $params = array() ) {
+ if ( isset( $params['logger'] ) ) {
+ $this->setLogger( $params['logger'] );
+ } else {
+ $this->setLogger( new NullLogger() );
+ }
+ }
+
+ /**
+ * @param LoggerInterface $logger
+ * @return null
+ */
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
/**
* @param bool $bool
*/
*/
public function debug( $text ) {
if ( $this->debugMode ) {
- $class = get_class( $this );
- wfDebug( "$class debug: $text\n" );
+ $this->logger->debug( "{class} debug: $text", array(
+ 'class' => get_class( $this ),
+ ) );
}
}
/** @var array */
protected $bag;
- function __construct() {
+ function __construct( $params = array() ) {
+ parent::__construct( $params );
$this->bag = array();
}
* @param string $text
*/
protected function debugLog( $text ) {
- wfDebugLog( 'memcached', $text );
+ $this->logger->debug( $text );
}
}
* @version 0.1.2
*/
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
// {{{ requirements
// }}}
*/
public $_connect_attempts;
+ /**
+ * @var LoggerInterface
+ */
+ private $_logger;
+
// }}}
// }}}
// {{{ methods
$this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
$this->_connect_attempts = 2;
+
+ $this->_logger = isset( $args['logger'] ) ? $args['logger'] : new NullLogger();
}
// }}}
* @param string $text
*/
function _debugprint( $text ) {
- wfDebugLog( 'memcached', $text );
+ $this->_logger->debug( $text );
}
/**
* @param string $text
*/
function _error_log( $text ) {
- wfDebugLog( 'memcached-serious', "Memcached error: $text" );
+ $this->_logger->error( "Memcached error: $text" );
}
/**
* @throws MWException
*/
function __construct( $params ) {
+ parent::__construct( $params );
$params = $this->applyDefaultParams( $params );
if ( $params['persistent'] ) {
// We can only reuse a pool ID if we keep the config consistent.
$this->client = new Memcached( md5( serialize( $params ) ) );
if ( count( $this->client->getServerList() ) ) {
- wfDebug( __METHOD__ . ": persistent Memcached object already loaded.\n" );
+ $this->logger->debug( __METHOD__ . ": persistent Memcached object already loaded." );
return; // already initialized; don't add duplicate servers
}
} else {
break;
default:
$msg = $this->client->getResultMessage();
+ $logCtx = array();
if ( $key !== false ) {
$server = $this->client->getServerByKey( $key );
- $serverName = "{$server['host']}:{$server['port']}";
- $msg = "Memcached error for key \"$key\" on server \"$serverName\": $msg";
+ $logCtx['memcached-server'] = "{$server['host']}:{$server['port']}";
+ $logCtx['memcached-key'] = $key;
+ $msg = "Memcached error for key \"{memcached-key}\" on server \"{memcached-server}\": $msg";
} else {
$msg = "Memcached error: $msg";
}
- wfDebugLog( 'memcached-serious', $msg );
+ $this->logger->error( $msg, $logCtx );
$this->setLastError( BagOStuff::ERR_UNEXPECTED );
}
return $result;
* @param array $params
*/
function __construct( $params ) {
+ parent::__construct( $params );
$params = $this->applyDefaultParams( $params );
$this->client = new MemCachedClientforWiki( $params );
* @throws MWException
*/
public function __construct( $params ) {
+ parent::__construct( $params );
if ( !isset( $params['caches'] ) ) {
throw new MWException( __METHOD__ . ': the caches parameter is required' );
}
* @return BagOStuff
*/
static function newFromParams( $params ) {
+ if ( isset( $params['loggroup'] ) ) {
+ $params['logger'] = MWLoggerFactory::getInstance( $params['loggroup'] );
+ } else {
+ // For backwards-compatability with custom parameters, lets not
+ // have all logging suddenly disappear
+ $params['logger'] = MWLoggerFactory::getInstance( 'objectcache' );
+ }
if ( isset( $params['factory'] ) ) {
return call_user_func( $params['factory'], $params );
} elseif ( isset( $params['class'] ) ) {
* @param array $params
*/
function __construct( $params ) {
+ parent::__construct( $params );
$redisConf = array( 'serializer' => 'none' ); // manage that in this class
foreach ( array( 'connectTimeout', 'persistent', 'password' ) as $opt ) {
if ( isset( $params[$opt] ) ) {
* @param string $msg
*/
protected function logError( $msg ) {
- wfDebugLog( 'redis', "Redis error: $msg" );
+ $this->logger->error( "Redis error: $msg" );
}
/**
* @param array $params
*/
public function __construct( $params ) {
+ parent::__construct( $params );
if ( isset( $params['servers'] ) ) {
$this->serverInfos = $params['servers'];
$this->numServers = count( $this->serverInfos );
# If server connection info was given, use that
if ( $this->serverInfos ) {
if ( $wgDebugDBTransactions ) {
- wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
+ $this->logger->debug( "Using provided serverInfo for SqlBagOStuff" );
}
$info = $this->serverInfos[$serverIndex];
$type = isset( $info['type'] ) ? $info['type'] : 'mysql';
$host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
- wfDebug( __CLASS__ . ": connecting to $host\n" );
+ $this->logger->debug( __CLASS__ . ": connecting to $host" );
$db = DatabaseBase::factory( $type, $info );
$db->clearFlag( DBO_TRX );
} else {
}
}
if ( $wgDebugDBTransactions ) {
- wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
+ $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
}
$this->conns[$serverIndex] = $db;
}
if ( $exception instanceof DBConnectionError ) {
$this->markServerDown( $exception, $serverIndex );
}
- wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
+ $this->logger->error( "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
$this->setLastError( BagOStuff::ERR_UNREACHABLE );
- wfDebug( __METHOD__ . ": ignoring connection error\n" );
+ $this->logger->debug( __METHOD__ . ": ignoring connection error" );
} else {
$this->setLastError( BagOStuff::ERR_UNEXPECTED );
- wfDebug( __METHOD__ . ": ignoring query error\n" );
+ $this->logger->debug( __METHOD__ . ": ignoring query error" );
}
}
} catch ( DBError $e ) {
}
}
- wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
+
+ $this->logger->error( "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
$this->setLastError( BagOStuff::ERR_UNREACHABLE );
- wfDebug( __METHOD__ . ": ignoring connection error\n" );
+ $this->logger->debug( __METHOD__ . ": ignoring connection error" );
} else {
$this->setLastError( BagOStuff::ERR_UNEXPECTED );
- wfDebug( __METHOD__ . ": ignoring query error\n" );
+ $this->logger->debug( __METHOD__ . ": ignoring query error" );
}
}
unset( $this->connFailureTimes[$serverIndex] );
unset( $this->connFailureErrors[$serverIndex] );
} else {
- wfDebug( __METHOD__ . ": Server #$serverIndex already down\n" );
+ $this->logger->debug( __METHOD__ . ": Server #$serverIndex already down" );
return;
}
}
$now = time();
- wfDebug( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) . "\n" );
+ $this->logger->info( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) );
$this->connFailureTimes[$serverIndex] = $now;
$this->connFailureErrors[$serverIndex] = $exception;
}