MediaWiki REL1_41
MonologSpi.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Logger;
22
23use DateTimeZone;
25use Monolog\Handler\StreamHandler;
26use Monolog\Logger;
27use Psr\Log\LoggerInterface;
28use Wikimedia\ObjectFactory\ObjectFactory;
29
119class MonologSpi implements Spi {
120
124 protected $singletons;
125
130 protected $config;
131
135 public function __construct( array $config ) {
136 $this->config = [];
137 $this->mergeConfig( $config );
138 }
139
146 public function mergeConfig( array $config ) {
147 foreach ( $config as $key => $value ) {
148 if ( isset( $this->config[$key] ) ) {
149 $this->config[$key] = array_merge( $this->config[$key], $value );
150 } else {
151 $this->config[$key] = $value;
152 }
153 }
154 if ( !isset( $this->config['loggers']['@default'] ) ) {
155 $this->config['loggers']['@default'] = [
156 'handlers' => [ '@default' ],
157 ];
158 if ( !isset( $this->config['handlers']['@default'] ) ) {
159 $this->config['handlers']['@default'] = [
160 'class' => StreamHandler::class,
161 'args' => [ 'php://stderr', Logger::ERROR ],
162 ];
163 }
164 }
165 $this->reset();
166 }
167
174 public function reset() {
175 $this->singletons = [
176 'loggers' => [],
177 'handlers' => [],
178 'formatters' => [],
179 'processors' => [],
180 ];
181 }
182
193 public function getLogger( $channel ) {
194 if ( !isset( $this->singletons['loggers'][$channel] ) ) {
195 // Fallback to using the '@default' configuration if an explicit
196 // configuration for the requested channel isn't found.
197 $spec = $this->config['loggers'][$channel] ?? $this->config['loggers']['@default'];
198
199 $monolog = $this->createLogger( $channel, $spec );
200 $this->singletons['loggers'][$channel] = $monolog;
201 }
202
203 return $this->singletons['loggers'][$channel];
204 }
205
212 protected function createLogger( $channel, $spec ): LoggerInterface {
213 $handlers = [];
214 if ( isset( $spec['handlers'] ) && $spec['handlers'] ) {
215 foreach ( $spec['handlers'] as $handler ) {
216 $handlers[] = $this->getHandler( $handler );
217 }
218 }
219
220 $processors = [];
221 if ( isset( $spec['processors'] ) ) {
222 foreach ( $spec['processors'] as $processor ) {
223 $processors[] = $this->getProcessor( $processor );
224 }
225 }
226
227 // Use UTC for logs instead of Monolog's default, which asks the
228 // PHP runtime, which MediaWiki sets to $wgLocaltimezone (T99581)
229 $obj = new Logger( $channel, $handlers, $processors, new DateTimeZone( 'UTC' ) );
230
231 if ( isset( $spec['calls'] ) ) {
232 foreach ( $spec['calls'] as $method => $margs ) {
233 $obj->$method( ...$margs );
234 }
235 }
236
237 return $obj;
238 }
239
245 public function getProcessor( $name ) {
246 if ( !isset( $this->singletons['processors'][$name] ) ) {
247 $spec = $this->config['processors'][$name];
248 $processor = ObjectFactory::getObjectFromSpec( $spec );
249 $this->singletons['processors'][$name] = $processor;
250 }
251 return $this->singletons['processors'][$name];
252 }
253
259 public function getHandler( $name ) {
260 if ( !isset( $this->singletons['handlers'][$name] ) ) {
261 $spec = $this->config['handlers'][$name];
262 $handler = ObjectFactory::getObjectFromSpec( $spec );
263 if (
264 isset( $spec['formatter'] ) &&
265 is_subclass_of( $handler, 'Monolog\Handler\FormattableHandlerInterface' )
266 ) {
267 $handler->setFormatter(
268 $this->getFormatter( $spec['formatter'] )
269 );
270 }
271 if ( isset( $spec['buffer'] ) && $spec['buffer'] ) {
272 $handler = new BufferHandler( $handler );
273 }
274 $this->singletons['handlers'][$name] = $handler;
275 }
276 return $this->singletons['handlers'][$name];
277 }
278
284 public function getFormatter( $name ) {
285 if ( !isset( $this->singletons['formatters'][$name] ) ) {
286 $spec = $this->config['formatters'][$name];
287 $formatter = ObjectFactory::getObjectFromSpec( $spec );
288 $this->singletons['formatters'][$name] = $formatter;
289 }
290 return $this->singletons['formatters'][$name];
291 }
292}
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.
Helper class for the index.php entry point.
Service provider interface to create \Psr\Log\LoggerInterface objects.
Definition Spi.php:64