MediaWiki  1.33.0
MonologSpi.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger;
22 
24 use Monolog\Logger;
25 use Monolog\Handler\StreamHandler;
26 use Wikimedia\ObjectFactory;
27 
116 class 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'] ) ) {
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 ( isset( $spec['formatter'] ) ) {
256  $handler->setFormatter(
257  $this->getFormatter( $spec['formatter'] )
258  );
259  }
260  if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
262  }
263  $this->singletons['handlers'][$name] = $handler;
264  }
265  return $this->singletons['handlers'][$name];
266  }
267 
273  public function getFormatter( $name ) {
274  if ( !isset( $this->singletons['formatters'][$name] ) ) {
275  $spec = $this->config['formatters'][$name];
276  $formatter = ObjectFactory::getObjectFromSpec( $spec );
277  $this->singletons['formatters'][$name] = $formatter;
278  }
279  return $this->singletons['formatters'][$name];
280  }
281 }
MediaWiki\Logger\MonologSpi\getFormatter
getFormatter( $name)
Create or return cached formatter.
Definition: MonologSpi.php:273
MediaWiki\Logger\MonologSpi
LoggerFactory service provider that creates loggers implemented by Monolog.
Definition: MonologSpi.php:116
MediaWiki\Logger\MonologSpi\$singletons
$singletons
Definition: MonologSpi.php:121
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Logger\MonologSpi\getProcessor
getProcessor( $name)
Create or return cached processor.
Definition: MonologSpi.php:237
MediaWiki\Logger
Definition: ConsoleLogger.php:3
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
MediaWiki\Logger\MonologSpi\$config
$config
Configuration for creating new loggers.
Definition: MonologSpi.php:127
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
MediaWiki\Logger\MonologSpi\__construct
__construct(array $config)
Definition: MonologSpi.php:132
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
$value
$value
Definition: styleTest.css.php:49
MediaWiki\Logger\MonologSpi\getLogger
getLogger( $channel)
Get a logger instance.
Definition: MonologSpi.php:190
MediaWiki\Logger\MonologSpi\getHandler
getHandler( $name)
Create or return cached handler.
Definition: MonologSpi.php:251
MediaWiki\Logger\MonologSpi\reset
reset()
Reset internal caches.
Definition: MonologSpi.php:171
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWiki\Logger\Monolog\BufferHandler
Updates \Monolog\Handler\BufferHandler to use DeferredUpdates rather than register_shutdown_function.
Definition: BufferHandler.php:35
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Logger\Spi
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition: Spi.php:36
MediaWiki\Logger\MonologSpi\mergeConfig
mergeConfig(array $config)
Merge additional configuration data into the configuration.
Definition: MonologSpi.php:143
MediaWiki\Logger\MonologSpi\createLogger
createLogger( $channel, $spec)
Create a logger.
Definition: MonologSpi.php:209