3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * MWLogger service provider that creates loggers implemented by Monolog.
24 * Configured using an array of configuration data with the keys 'loggers',
25 * 'processors', 'handlers' and 'formatters'.
27 * The ['loggers']['@default'] configuration will be used to create loggers
28 * for any channel that isn't explicitly named in the 'loggers' configuration
31 * Configuration will most typically be provided in the $wgMWLoggerDefaultSpi
32 * global configuration variable used by MWLogger to construct its default SPI
35 * $wgMWLoggerDefaultSpi = array(
36 * 'class' => 'MWLoggerMonologSpi',
37 * 'args' => array( array(
39 * '@default' => array(
40 * 'processors' => array( 'wiki', 'psr', 'pid', 'uid', 'web' ),
41 * 'handlers' => array( 'stream' ),
44 * 'processors' => array( 'wiki', 'psr', 'pid' ),
45 * 'handlers' => array( 'stream' ),
48 * 'processors' => array(
50 * 'class' => 'MWLoggerMonologProcessor',
53 * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor',
56 * 'class' => '\\Monolog\\Processor\\ProcessIdProcessor',
59 * 'class' => '\\Monolog\\Processor\\UidProcessor',
62 * 'class' => '\\Monolog\\Processor\\WebProcessor',
65 * 'handlers' => array(
67 * 'class' => '\\Monolog\\Handler\\StreamHandler',
68 * 'args' => array( 'path/to/your.log' ),
69 * 'formatter' => 'line',
72 * 'class' => '\\Monolog\\Handler\\RedisHandler',
73 * 'args' => array( function() {
74 * $redis = new Redis();
75 * $redis->connect( '127.0.0.1', 6379 );
80 * 'formatter' => 'logstash',
83 * 'class' => 'MWLoggerMonologHandler',
85 * 'udp://127.0.0.1:8420/mediawiki
87 * 'formatter' => 'line',
90 * 'formatters' => array(
92 * 'class' => '\\Monolog\\Formatter\\LineFormatter',
94 * 'logstash' => array(
95 * 'class' => '\\Monolog\\Formatter\\LogstashFormatter',
96 * 'args' => array( 'mediawiki', php_uname( 'n' ), null, '', 1 ),
103 * @see https://github.com/Seldaek/monolog
105 * @author Bryan Davis <bd808@wikimedia.org>
106 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
108 class MWLoggerMonologSpi
implements MWLoggerSpi
{
111 * @var array $singletons
113 protected $singletons;
116 * Configuration for creating new loggers.
123 * @param array $config Configuration data.
125 public function __construct( array $config ) {
126 $this->config
= $config;
132 * Reset internal caches.
134 * This is public for use in unit tests. Under normal operation there should
135 * be no need to flush the caches.
137 public function reset() {
138 $this->singletons
= array(
139 'loggers' => array(),
140 'handlers' => array(),
141 'formatters' => array(),
142 'processors' => array(),
148 * Get a logger instance.
150 * Creates and caches a logger instance based on configuration found in the
151 * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel
152 * name will return the cached instance.
154 * @param string $channel Logging channel
155 * @return MWLogger Logger instance
157 public function getLogger( $channel ) {
158 if ( !isset( $this->singletons
['loggers'][$channel] ) ) {
159 // Fallback to using the '@default' configuration if an explict
160 // configuration for the requested channel isn't found.
161 $spec = isset( $this->config
['loggers'][$channel] ) ?
162 $this->config
['loggers'][$channel] :
163 $this->config
['loggers']['@default'];
165 $monolog = $this->createLogger( $channel, $spec );
166 $this->singletons
['loggers'][$channel] = new MWLogger( $monolog );
169 return $this->singletons
['loggers'][$channel];
175 * @param string $channel Logger channel
176 * @param array $spec Configuration
177 * @return \Monolog\Logger
179 protected function createLogger( $channel, $spec ) {
180 $obj = new \Monolog\
Logger( $channel );
182 if ( isset( $spec['processors'] ) ) {
183 foreach ( $spec['processors'] as $processor ) {
184 $obj->pushProcessor( $this->getProcessor( $processor ) );
188 if ( isset( $spec['handlers'] ) ) {
189 foreach ( $spec['handlers'] as $handler ) {
190 $obj->pushHandler( $this->getHandler( $handler ) );
198 * Create or return cached processor.
199 * @param string $name Processor name
202 protected function getProcessor( $name ) {
203 if ( !isset( $this->singletons
['processors'][$name] ) ) {
204 $spec = $this->config
['processors'][$name];
205 $processor = ObjectFactory
::getObjectFromSpec( $spec );
206 $this->singletons
['processors'][$name] = $processor;
208 return $this->singletons
['processors'][$name];
213 * Create or return cached handler.
214 * @param string $name Processor name
215 * @return \Monolog\Handler\HandlerInterface
217 protected function getHandler( $name ) {
218 if ( !isset( $this->singletons
['handlers'][$name] ) ) {
219 $spec = $this->config
['handlers'][$name];
220 $handler = ObjectFactory
::getObjectFromSpec( $spec );
221 $handler->setFormatter( $this->getFormatter( $spec['formatter'] ) );
222 $this->singletons
['handlers'][$name] = $handler;
224 return $this->singletons
['handlers'][$name];
229 * Create or return cached formatter.
230 * @param string $name Formatter name
231 * @return \Monolog\Formatter\FormatterInterface
233 protected function getFormatter( $name ) {
234 if ( !isset( $this->singletons
['formatters'][$name] ) ) {
235 $spec = $this->config
['formatters'][$name];
236 $formatter = ObjectFactory
::getObjectFromSpec( $spec );
237 $this->singletons
['formatters'][$name] = $formatter;
239 return $this->singletons
['formatters'][$name];