MediaWiki REL1_32
MonologSpi.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Logger;
22
24use Monolog\Logger;
25use Wikimedia\ObjectFactory;
26
115class MonologSpi implements Spi {
116
120 protected $singletons;
121
126 protected $config;
127
131 public function __construct( array $config ) {
132 $this->config = [];
133 $this->mergeConfig( $config );
134 }
135
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 );
146 } else {
147 $this->config[$key] = $value;
148 }
149 }
150 $this->reset();
151 }
152
159 public function reset() {
160 $this->singletons = [
161 'loggers' => [],
162 'handlers' => [],
163 'formatters' => [],
164 'processors' => [],
165 ];
166 }
167
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'];
183
184 $monolog = $this->createLogger( $channel, $spec );
185 $this->singletons['loggers'][$channel] = $monolog;
186 }
187
188 return $this->singletons['loggers'][$channel];
189 }
190
197 protected function createLogger( $channel, $spec ) {
198 $obj = new Logger( $channel );
199
200 if ( isset( $spec['calls'] ) ) {
201 foreach ( $spec['calls'] as $method => $margs ) {
202 $obj->$method( ...$margs );
203 }
204 }
205
206 if ( isset( $spec['processors'] ) ) {
207 foreach ( $spec['processors'] as $processor ) {
208 $obj->pushProcessor( $this->getProcessor( $processor ) );
209 }
210 }
211
212 if ( isset( $spec['handlers'] ) ) {
213 foreach ( $spec['handlers'] as $handler ) {
214 $obj->pushHandler( $this->getHandler( $handler ) );
215 }
216 }
217 return $obj;
218 }
219
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;
230 }
231 return $this->singletons['processors'][$name];
232 }
233
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'] )
246 );
247 }
248 if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
250 }
251 $this->singletons['handlers'][$name] = $handler;
252 }
253 return $this->singletons['handlers'][$name];
254 }
255
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;
266 }
267 return $this->singletons['formatters'][$name];
268 }
269}
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.
createLogger( $channel, $spec)
Create a logger.
$config
Configuration for creating new loggers.
getFormatter( $name)
Create or return cached formatter.
Updates \Monolog\Handler\BufferHandler to use DeferredUpdates rather than register_shutdown_function.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:933
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition Spi.php:36
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))