array( * '@default' => array( * 'processors' => array( 'wiki', 'psr', 'pid', 'uid', 'web' ), * 'handlers' => array( 'stream' ), * ), * 'runJobs' => array( * 'processors' => array( 'wiki', 'psr', 'pid' ), * 'handlers' => array( 'stream' ), * ) * ), * 'processors' => array( * 'wiki' => array( * 'class' => 'MWLoggerMonologProcessor', * ), * 'psr' => array( * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor', * ), * 'pid' => array( * 'class' => '\\Monolog\\Processor\\ProcessIdProcessor', * ), * 'uid' => array( * 'class' => '\\Monolog\\Processor\\UidProcessor', * ), * 'web' => array( * 'class' => '\\Monolog\\Processor\\WebProcessor', * ), * ), * 'handlers' => array( * 'stream' => array( * 'class' => '\\Monolog\\Handler\\StreamHandler', * 'args' => array( 'path/to/your.log' ), * 'formatter' => 'line', * ), * 'redis' => array( * 'class' => '\\Monolog\\Handler\\RedisHandler', * 'args' => array( function() { * $redis = new Redis(); * $redis->connect( '127.0.0.1', 6379 ); * return $redis; * }, * 'logstash' * ), * 'formatter' => 'logstash', * ), * 'udp2log' => array( * 'class' => 'MWLoggerMonologHandler', * 'args' => array( * 'udp://127.0.0.1:8420/mediawiki * ), * 'formatter' => 'line', * ), * ), * 'formatters' => array( * 'line' => array( * 'class' => '\\Monolog\\Formatter\\LineFormatter', * ), * 'logstash' => array( * 'class' => '\\Monolog\\Formatter\\LogstashFormatter', * 'args' => array( 'mediawiki', php_uname( 'n' ), null, '', 1 ), * ), * ), * ); * @endcode * * @see https://github.com/Seldaek/monolog * @since 1.25 * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ class MWLoggerMonologSpi implements MWLoggerSpi { /** * @var array $singletons */ protected $singletons; /** * Configuration for creating new loggers. * @var array $config */ protected $config; /** * @param array $config Configuration data. Defaults to global * $wgMWLoggerMonologSpiConfig */ public function __construct( $config = null ) { if ( $config === null ) { global $wgMWLoggerMonologSpiConfig; $config = $wgMWLoggerMonologSpiConfig; } $this->config = $config; $this->reset(); } /** * Reset internal caches. * * This is public for use in unit tests. Under normal operation there should * be no need to flush the caches. */ public function reset() { $this->singletons = array( 'loggers' => array(), 'handlers' => array(), 'formatters' => array(), 'processors' => array(), ); } /** * Get a logger instance. * * Creates and caches a logger instance based on configuration found in the * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel * name will return the cached instance. * * @param string $channel Logging channel * @return MWLogger Logger instance */ public function getLogger( $channel ) { if ( !isset( $this->singletons['loggers'][$channel] ) ) { // Fallback to using the '@default' configuration if an explict // configuration for the requested channel isn't found. $spec = isset( $this->config['loggers'][$channel] ) ? $this->config['loggers'][$channel] : $this->config['loggers']['@default']; $monolog = $this->createLogger( $channel, $spec ); $this->singletons['loggers'][$channel] = new MWLogger( $monolog ); } return $this->singletons['loggers'][$channel]; } /** * Create a logger. * @param string $channel Logger channel * @param array $spec Configuration * @return \Monolog\Logger */ protected function createLogger( $channel, $spec ) { $obj = new \Monolog\Logger( $channel ); if ( isset( $spec['processors'] ) ) { foreach ( $spec['processors'] as $processor ) { $obj->pushProcessor( $this->getProcessor( $processor ) ); } } if ( isset( $spec['handlers'] ) ) { foreach ( $spec['handlers'] as $handler ) { $obj->pushHandler( $this->getHandler( $handler ) ); } } return $obj; } /** * Create or return cached processor. * @param string $name Processor name * @return callable */ protected function getProcessor( $name ) { if ( !isset( $this->singletons['processors'][$name] ) ) { $spec = $this->config['processors'][$name]; $this->singletons['processors'][$name] = $this->instantiate( $spec ); } return $this->singletons['processors'][$name]; } /** * Create or return cached handler. * @param string $name Processor name * @return \Monolog\Handler\HandlerInterface */ protected function getHandler( $name ) { if ( !isset( $this->singletons['handlers'][$name] ) ) { $spec = $this->config['handlers'][$name]; $handler = $this->instantiate( $spec ); $handler->setFormatter( $this->getFormatter( $spec['formatter'] ) ); $this->singletons['handlers'][$name] = $handler; } return $this->singletons['handlers'][$name]; } /** * Create or return cached formatter. * @param string $name Formatter name * @return \Monolog\Formatter\FormatterInterface */ protected function getFormatter( $name ) { if ( !isset( $this->singletons['formatters'][$name] ) ) { $spec = $this->config['formatters'][$name]; $this->singletons['formatters'][$name] = $this->instantiate( $spec ); } return $this->singletons['formatters'][$name]; } /** * Instantiate the requested object. * * The specification array must contain a 'class' key with string value that * specifies the class name to instantiate. It can optionally contain an * 'args' key that provides constructor arguments. * * @param array $spec Object specification * @return object */ protected function instantiate( $spec ) { $clazz = $spec['class']; $args = isset( $spec['args'] ) ? $spec['args'] : array(); // If an argument is a callable, call it. // This allows passing things such as a database connection to a logger. $args = array_map( function ( $value ) { if ( is_callable( $value ) ) { return $value(); } else { return $value; } }, $args ); if ( empty( $args ) ) { $obj = new $clazz(); } else { $ref = new ReflectionClass( $clazz ); $obj = $ref->newInstanceArgs( $args ); } return $obj; } }