* @copyright © 2014 Bryan Davis and Wikimedia Foundation.
*/
class LoggerFactory {
/**
* Service provider.
* @var Spi $spi
*/
private static $spi;
/**
* Register a service provider to create new \Psr\Log\LoggerInterface
* instances.
*
* @param Spi $provider Provider to register
*/
public static function registerProvider( Spi $provider ) {
self::$spi = $provider;
}
/**
* Get the registered service provider.
*
* If called before any service provider has been registered, it will
* attempt to use the $wgMWLoggerDefaultSpi global to bootstrap
* Spi registration. $wgMWLoggerDefaultSpi is expected to be an
* array usable by ObjectFactory::getObjectFromSpec() to create a class.
*
* @return Spi
* @see registerProvider()
* @see ObjectFactory::getObjectFromSpec()
*/
public static function getProvider() {
if ( self::$spi === null ) {
global $wgMWLoggerDefaultSpi;
$provider = ObjectFactory::getObjectFromSpec(
$wgMWLoggerDefaultSpi
);
self::registerProvider( $provider );
}
return self::$spi;
}
/**
* Get a named logger instance from the currently configured logger factory.
*
* @param string $channel Logger channel (name)
* @return \Psr\Log\LoggerInterface
*/
public static function getInstance( $channel ) {
if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
$message = (
'MediaWiki requires the PSR-3 logging ' .
"library to be present. This library is not embedded directly in MediaWiki's " .
"git repository and must be installed separately by the end user.\n\n" .
'Please see mediawiki.org for help on installing ' .
'the required components.'
);
echo $message;
trigger_error( $message, E_USER_ERROR );
die( 1 );
}
return self::getProvider()->getLogger( $channel );
}
/**
* Construction of utility class is not allowed.
*/
private function __construct() {
// no-op
}
}