MediaWiki REL1_37
MonologSpi.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Logger;
22
24use Monolog\Handler\StreamHandler;
25use Monolog\Logger;
26use Wikimedia\ObjectFactory;
27
116class MonologSpi implements Spi {
117
121 protected $singletons;
122
127 protected $config;
128
132 public function __construct( array $config ) {
133 $this->config = [];
134 $this->mergeConfig( $config );
135 }
136
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 );
147 } else {
148 $this->config[$key] = $value;
149 }
150 }
151 if ( !isset( $this->config['loggers']['@default'] ) ) {
152 $this->config['loggers']['@default'] = [
153 'handlers' => [ '@default' ],
154 ];
155 if ( !isset( $this->config['handlers']['@default'] ) ) {
156 $this->config['handlers']['@default'] = [
157 'class' => StreamHandler::class,
158 'args' => [ 'php://stderr', Logger::ERROR ],
159 ];
160 }
161 }
162 $this->reset();
163 }
164
171 public function reset() {
172 $this->singletons = [
173 'loggers' => [],
174 'handlers' => [],
175 'formatters' => [],
176 'processors' => [],
177 ];
178 }
179
190 public function getLogger( $channel ) {
191 if ( !isset( $this->singletons['loggers'][$channel] ) ) {
192 // Fallback to using the '@default' configuration if an explict
193 // configuration for the requested channel isn't found.
194 $spec = $this->config['loggers'][$channel] ?? $this->config['loggers']['@default'];
195
196 $monolog = $this->createLogger( $channel, $spec );
197 $this->singletons['loggers'][$channel] = $monolog;
198 }
199
200 return $this->singletons['loggers'][$channel];
201 }
202
209 protected function createLogger( $channel, $spec ) {
210 $obj = new Logger( $channel );
211
212 if ( isset( $spec['calls'] ) ) {
213 foreach ( $spec['calls'] as $method => $margs ) {
214 $obj->$method( ...$margs );
215 }
216 }
217
218 if ( isset( $spec['processors'] ) ) {
219 foreach ( $spec['processors'] as $processor ) {
220 $obj->pushProcessor( $this->getProcessor( $processor ) );
221 }
222 }
223
224 if ( isset( $spec['handlers'] ) && $spec['handlers'] ) {
225 foreach ( $spec['handlers'] as $handler ) {
226 $obj->pushHandler( $this->getHandler( $handler ) );
227 }
228 }
229 return $obj;
230 }
231
237 public function getProcessor( $name ) {
238 if ( !isset( $this->singletons['processors'][$name] ) ) {
239 $spec = $this->config['processors'][$name];
240 $processor = ObjectFactory::getObjectFromSpec( $spec );
241 $this->singletons['processors'][$name] = $processor;
242 }
243 return $this->singletons['processors'][$name];
244 }
245
251 public function getHandler( $name ) {
252 if ( !isset( $this->singletons['handlers'][$name] ) ) {
253 $spec = $this->config['handlers'][$name];
254 $handler = ObjectFactory::getObjectFromSpec( $spec );
255 if (
256 isset( $spec['formatter'] ) &&
257 is_subclass_of( $handler, 'Monolog\Handler\FormattableHandlerInterface' )
258 ) {
259 $handler->setFormatter(
260 $this->getFormatter( $spec['formatter'] )
261 );
262 }
263 if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
264 $handler = new BufferHandler( $handler );
265 }
266 $this->singletons['handlers'][$name] = $handler;
267 }
268 return $this->singletons['handlers'][$name];
269 }
270
276 public function getFormatter( $name ) {
277 if ( !isset( $this->singletons['formatters'][$name] ) ) {
278 $spec = $this->config['formatters'][$name];
279 $formatter = ObjectFactory::getObjectFromSpec( $spec );
280 $this->singletons['formatters'][$name] = $formatter;
281 }
282 return $this->singletons['formatters'][$name];
283 }
284}
LoggerFactory service provider that creates loggers implemented by Monolog.
getHandler( $name)
Create or return cached handler.
getProcessor( $name)
Create or return cached processor.
mergeConfig(array $config)
Merge additional configuration data into the configuration.
reset()
Reset internal caches.
getLogger( $channel)
Get a logger instance.
array[][] $config
Configuration for creating new loggers.
createLogger( $channel, $spec)
Create a logger.
getFormatter( $name)
Create or return cached formatter.
Updates \Monolog\Handler\BufferHandler to use DeferredUpdates rather than register_shutdown_function.
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition Spi.php:38