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