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
21 namespace MediaWiki\Logger
;
23 use MediaWiki\Logger\Monolog\BufferHandler
;
28 * LoggerFactory service provider that creates loggers implemented by
31 * Configured using an array of configuration data with the keys 'loggers',
32 * 'processors', 'handlers' and 'formatters'.
34 * The ['loggers']['\@default'] configuration will be used to create loggers
35 * for any channel that isn't explicitly named in the 'loggers' configuration
38 * Configuration will most typically be provided in the $wgMWLoggerDefaultSpi
39 * global configuration variable used by LoggerFactory to construct its
40 * default SPI provider:
42 * $wgMWLoggerDefaultSpi = array(
43 * 'class' => '\\MediaWiki\\Logger\\MonologSpi',
44 * 'args' => array( array(
46 * '@default' => array(
47 * 'processors' => array( 'wiki', 'psr', 'pid', 'uid', 'web' ),
48 * 'handlers' => array( 'stream' ),
51 * 'processors' => array( 'wiki', 'psr', 'pid' ),
52 * 'handlers' => array( 'stream' ),
55 * 'processors' => array(
57 * 'class' => '\\MediaWiki\\Logger\\Monolog\\WikiProcessor',
60 * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor',
63 * 'class' => '\\Monolog\\Processor\\ProcessIdProcessor',
66 * 'class' => '\\Monolog\\Processor\\UidProcessor',
69 * 'class' => '\\Monolog\\Processor\\WebProcessor',
72 * 'handlers' => array(
74 * 'class' => '\\Monolog\\Handler\\StreamHandler',
75 * 'args' => array( 'path/to/your.log' ),
76 * 'formatter' => 'line',
79 * 'class' => '\\Monolog\\Handler\\RedisHandler',
80 * 'args' => array( function() {
81 * $redis = new Redis();
82 * $redis->connect( '127.0.0.1', 6379 );
87 * 'formatter' => 'logstash',
91 * 'class' => '\\MediaWiki\\Logger\\Monolog\\LegacyHandler',
93 * 'udp://127.0.0.1:8420/mediawiki
95 * 'formatter' => 'line',
98 * 'formatters' => array(
100 * 'class' => '\\Monolog\\Formatter\\LineFormatter',
102 * 'logstash' => array(
103 * 'class' => '\\Monolog\\Formatter\\LogstashFormatter',
104 * 'args' => array( 'mediawiki', php_uname( 'n' ), null, '', 1 ),
111 * @see https://github.com/Seldaek/monolog
113 * @author Bryan Davis <bd808@wikimedia.org>
114 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
116 class MonologSpi
implements Spi
{
119 * @var array $singletons
121 protected $singletons;
124 * Configuration for creating new loggers.
130 * @param array $config Configuration data.
132 public function __construct( array $config ) {
133 $this->config
= array();
134 $this->mergeConfig( $config );
138 * Merge additional configuration data into the configuration.
141 * @param array $config Configuration data.
143 public function mergeConfig( array $config ) {
144 foreach ( $config as $key => $value ) {
145 if ( isset( $this->config
[$key] ) ) {
146 $this->config
[$key] = array_merge( $this->config
[$key], $value );
148 $this->config
[$key] = $value;
155 * Reset internal caches.
157 * This is public for use in unit tests. Under normal operation there should
158 * be no need to flush the caches.
160 public function reset() {
161 $this->singletons
= array(
162 'loggers' => array(),
163 'handlers' => array(),
164 'formatters' => array(),
165 'processors' => array(),
170 * Get a logger instance.
172 * Creates and caches a logger instance based on configuration found in the
173 * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel
174 * name will return the cached instance.
176 * @param string $channel Logging channel
177 * @return \Psr\Log\LoggerInterface Logger instance
179 public function getLogger( $channel ) {
180 if ( !isset( $this->singletons
['loggers'][$channel] ) ) {
181 // Fallback to using the '@default' configuration if an explict
182 // configuration for the requested channel isn't found.
183 $spec = isset( $this->config
['loggers'][$channel] ) ?
184 $this->config
['loggers'][$channel] :
185 $this->config
['loggers']['@default'];
187 $monolog = $this->createLogger( $channel, $spec );
188 $this->singletons
['loggers'][$channel] = $monolog;
191 return $this->singletons
['loggers'][$channel];
196 * @param string $channel Logger channel
197 * @param array $spec Configuration
198 * @return \Monolog\Logger
200 protected function createLogger( $channel, $spec ) {
201 $obj = new Logger( $channel );
203 if ( isset( $spec['calls'] ) ) {
204 foreach ( $spec['calls'] as $method => $margs ) {
205 call_user_func_array( array( $obj, $method ), $margs );
209 if ( isset( $spec['processors'] ) ) {
210 foreach ( $spec['processors'] as $processor ) {
211 $obj->pushProcessor( $this->getProcessor( $processor ) );
215 if ( isset( $spec['handlers'] ) ) {
216 foreach ( $spec['handlers'] as $handler ) {
217 $obj->pushHandler( $this->getHandler( $handler ) );
224 * Create or return cached processor.
225 * @param string $name Processor name
228 public function getProcessor( $name ) {
229 if ( !isset( $this->singletons
['processors'][$name] ) ) {
230 $spec = $this->config
['processors'][$name];
231 $processor = ObjectFactory
::getObjectFromSpec( $spec );
232 $this->singletons
['processors'][$name] = $processor;
234 return $this->singletons
['processors'][$name];
238 * Create or return cached handler.
239 * @param string $name Processor name
240 * @return \Monolog\Handler\HandlerInterface
242 public function getHandler( $name ) {
243 if ( !isset( $this->singletons
['handlers'][$name] ) ) {
244 $spec = $this->config
['handlers'][$name];
245 $handler = ObjectFactory
::getObjectFromSpec( $spec );
246 if ( isset( $spec['formatter'] ) ) {
247 $handler->setFormatter(
248 $this->getFormatter( $spec['formatter'] )
251 if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
252 $handler = new BufferHandler( $handler );
254 $this->singletons
['handlers'][$name] = $handler;
256 return $this->singletons
['handlers'][$name];
260 * Create or return cached formatter.
261 * @param string $name Formatter name
262 * @return \Monolog\Formatter\FormatterInterface
264 public function getFormatter( $name ) {
265 if ( !isset( $this->singletons
['formatters'][$name] ) ) {
266 $spec = $this->config
['formatters'][$name];
267 $formatter = ObjectFactory
::getObjectFromSpec( $spec );
268 $this->singletons
['formatters'][$name] = $formatter;
270 return $this->singletons
['formatters'][$name];