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
;
25 use Wikimedia\ObjectFactory
;
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 = [
43 * 'class' => \MediaWiki\Logger\MonologSpi::class,
47 * 'processors' => [ 'wiki', 'psr', 'pid', 'uid', 'web' ],
48 * 'handlers' => [ 'stream' ],
51 * 'processors' => [ 'wiki', 'psr', 'pid' ],
52 * 'handlers' => [ 'stream' ],
57 * 'class' => \MediaWiki\Logger\Monolog\WikiProcessor::class,
60 * 'class' => \Monolog\Processor\PsrLogMessageProcessor::class,
63 * 'class' => \Monolog\Processor\ProcessIdProcessor::class,
66 * 'class' => \Monolog\Processor\UidProcessor::class,
69 * 'class' => \Monolog\Processor\WebProcessor::class,
74 * 'class' => \Monolog\Handler\StreamHandler::class,
75 * 'args' => [ 'path/to/your.log' ],
76 * 'formatter' => 'line',
79 * 'class' => \Monolog\Handler\RedisHandler::class,
80 * 'args' => [ function() {
81 * $redis = new Redis();
82 * $redis->connect( '127.0.0.1', 6379 );
87 * 'formatter' => 'logstash',
91 * 'class' => \MediaWiki\Logger\Monolog\LegacyHandler::class,
93 * 'udp://127.0.0.1:8420/mediawiki
95 * 'formatter' => 'line',
100 * 'class' => \Monolog\Formatter\LineFormatter::class,
103 * 'class' => \Monolog\Formatter\LogstashFormatter::class,
104 * 'args' => [ 'mediawiki', php_uname( 'n' ), null, '', 1 ],
111 * @see https://github.com/Seldaek/monolog
113 * @copyright © 2014 Wikimedia Foundation and contributors
115 class MonologSpi
implements Spi
{
118 * @var array $singletons
120 protected $singletons;
123 * Configuration for creating new loggers.
129 * @param array $config Configuration data.
131 public function __construct( array $config ) {
133 $this->mergeConfig( $config );
137 * Merge additional configuration data into the configuration.
140 * @param array $config Configuration data.
142 public function mergeConfig( array $config ) {
143 foreach ( $config as $key => $value ) {
144 if ( isset( $this->config
[$key] ) ) {
145 $this->config
[$key] = array_merge( $this->config
[$key], $value );
147 $this->config
[$key] = $value;
154 * Reset internal caches.
156 * This is public for use in unit tests. Under normal operation there should
157 * be no need to flush the caches.
159 public function reset() {
160 $this->singletons
= [
169 * Get a logger instance.
171 * Creates and caches a logger instance based on configuration found in the
172 * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel
173 * name will return the cached instance.
175 * @param string $channel Logging channel
176 * @return \Psr\Log\LoggerInterface Logger instance
178 public function getLogger( $channel ) {
179 if ( !isset( $this->singletons
['loggers'][$channel] ) ) {
180 // Fallback to using the '@default' configuration if an explict
181 // configuration for the requested channel isn't found.
182 $spec = $this->config
['loggers'][$channel] ??
$this->config
['loggers']['@default'];
184 $monolog = $this->createLogger( $channel, $spec );
185 $this->singletons
['loggers'][$channel] = $monolog;
188 return $this->singletons
['loggers'][$channel];
193 * @param string $channel Logger channel
194 * @param array $spec Configuration
195 * @return \Monolog\Logger
197 protected function createLogger( $channel, $spec ) {
198 $obj = new Logger( $channel );
200 if ( isset( $spec['calls'] ) ) {
201 foreach ( $spec['calls'] as $method => $margs ) {
202 call_user_func_array( [ $obj, $method ], $margs );
206 if ( isset( $spec['processors'] ) ) {
207 foreach ( $spec['processors'] as $processor ) {
208 $obj->pushProcessor( $this->getProcessor( $processor ) );
212 if ( isset( $spec['handlers'] ) ) {
213 foreach ( $spec['handlers'] as $handler ) {
214 $obj->pushHandler( $this->getHandler( $handler ) );
221 * Create or return cached processor.
222 * @param string $name Processor name
225 public function getProcessor( $name ) {
226 if ( !isset( $this->singletons
['processors'][$name] ) ) {
227 $spec = $this->config
['processors'][$name];
228 $processor = ObjectFactory
::getObjectFromSpec( $spec );
229 $this->singletons
['processors'][$name] = $processor;
231 return $this->singletons
['processors'][$name];
235 * Create or return cached handler.
236 * @param string $name Processor name
237 * @return \Monolog\Handler\HandlerInterface
239 public function getHandler( $name ) {
240 if ( !isset( $this->singletons
['handlers'][$name] ) ) {
241 $spec = $this->config
['handlers'][$name];
242 $handler = ObjectFactory
::getObjectFromSpec( $spec );
243 if ( isset( $spec['formatter'] ) ) {
244 $handler->setFormatter(
245 $this->getFormatter( $spec['formatter'] )
248 if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
249 $handler = new BufferHandler( $handler );
251 $this->singletons
['handlers'][$name] = $handler;
253 return $this->singletons
['handlers'][$name];
257 * Create or return cached formatter.
258 * @param string $name Formatter name
259 * @return \Monolog\Formatter\FormatterInterface
261 public function getFormatter( $name ) {
262 if ( !isset( $this->singletons
['formatters'][$name] ) ) {
263 $spec = $this->config
['formatters'][$name];
264 $formatter = ObjectFactory
::getObjectFromSpec( $spec );
265 $this->singletons
['formatters'][$name] = $formatter;
267 return $this->singletons
['formatters'][$name];