MediaWiki master
LegacyLogger.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Logger;
8
9use DateTimeZone;
10use Error;
11use LogicException;
15use Monolog\LogRecord;
16use Psr\Log\AbstractLogger;
17use Psr\Log\LogLevel;
18use Throwable;
19use Wikimedia\Timestamp\ConvertibleTimestamp;
20use Wikimedia\Timestamp\TimestampFormat as TS;
22
40class LegacyLogger extends AbstractLogger {
41
45 protected $channel;
46
47 private const LEVEL_DEBUG = 100;
48 private const LEVEL_INFO = 200;
49 private const LEVEL_NOTICE = 250;
50 private const LEVEL_WARNING = 300;
51 private const LEVEL_ERROR = 400;
52 private const LEVEL_CRITICAL = 500;
53 private const LEVEL_ALERT = 550;
54 private const LEVEL_EMERGENCY = 600;
55 private const LEVEL_INFINITY = 999;
56
63 protected static $levelMapping = [
64 LogLevel::DEBUG => self::LEVEL_DEBUG,
65 LogLevel::INFO => self::LEVEL_INFO,
66 LogLevel::NOTICE => self::LEVEL_NOTICE,
67 LogLevel::WARNING => self::LEVEL_WARNING,
68 LogLevel::ERROR => self::LEVEL_ERROR,
69 LogLevel::CRITICAL => self::LEVEL_CRITICAL,
70 LogLevel::ALERT => self::LEVEL_ALERT,
71 LogLevel::EMERGENCY => self::LEVEL_EMERGENCY,
72 ];
73
80 private $minimumLevel;
81
87 private $isDB;
88
92 public function __construct( $channel ) {
94
95 $this->channel = $channel;
96 $this->isDB = ( $channel === 'rdbms' );
97
98 // Calculate minimum level, duplicating some of the logic from log() and shouldEmit()
100 $this->minimumLevel = self::LEVEL_WARNING;
101 } elseif ( $wgDebugLogFile != '' || $wgShowDebug || $wgDebugToolbar ) {
102 // Log all messages if there is a debug log file or debug toolbar
103 $this->minimumLevel = self::LEVEL_DEBUG;
104 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
105 $logConfig = $wgDebugLogGroups[$channel];
106 // Log messages if the config is set, according to the configured level
107 if ( is_array( $logConfig ) && isset( $logConfig['level'] ) ) {
108 $this->minimumLevel = self::$levelMapping[$logConfig['level']];
109 } else {
110 $this->minimumLevel = self::LEVEL_DEBUG;
111 }
112 } else {
113 // No other case hit: discard all messages
114 $this->minimumLevel = self::LEVEL_INFINITY;
115 }
116
117 if ( $this->isDB && $wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
118 // Log DB errors if there is a DB error log
119 $this->minimumLevel = self::LEVEL_ERROR;
120 }
121 }
122
130 public function setMinimumForTest( ?int $level ) {
131 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
132 throw new LogicException( 'Not allowed outside tests' );
133 }
134 // Set LEVEL_INFINITY if given null, or restore the original level.
135 $original = $this->minimumLevel;
136 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
137 return $original;
138 }
139
147 public function log( $level, $message, array $context = [] ): void {
148 if ( is_string( $level ) ) {
149 $level = self::$levelMapping[$level];
150 }
151 if ( $level < $this->minimumLevel ) {
152 return;
153 }
154
155 $context += LoggerFactory::getContext()->get();
156
157 if ( $this->isDB
158 && $level === self::LEVEL_DEBUG
159 && isset( $context['sql'] )
160 ) {
161 // Also give the query information to the MWDebug tools
162 MWDebug::query(
163 $context['sql'],
164 $context['method'],
165 $context['runtime_ms'] / 1000,
166 $context['db_server'],
167 $context['rows'],
168 );
169 }
170
171 // If this is a DB-related error, and the site has $wgDBerrorLog
172 // configured, rewrite the channel as wfLogDBError instead.
173 // Likewise, if the site does not use $wgDBerrorLog, it should
174 // configurable like any other channel via $wgDebugLogGroups
175 // or $wgMWLoggerDefaultSpi.
176 global $wgDBerrorLog;
177 if ( $this->isDB && $level >= self::LEVEL_ERROR && $wgDBerrorLog ) {
178 // Format and write DB errors to the legacy locations
179 $effectiveChannel = 'wfLogDBError';
180 } else {
181 $effectiveChannel = $this->channel;
182 }
183
184 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
185 $text = self::format( $effectiveChannel, $message, $context );
186 $destination = self::destination( $effectiveChannel, $message, $context );
187 $this->maybeLogToStderr( $text );
188 self::emit( $text, $destination );
189 }
190 if ( !isset( $context['private'] ) || !$context['private'] ) {
191 // Add to debug toolbar if not marked as "private"
192 MWDebug::debugMsg( $message, [ 'channel' => $this->channel ] + $context );
193 }
194 }
195
206 public static function shouldEmit( $channel, $message, $level, $context ) {
208
209 if ( is_string( $level ) ) {
210 $level = self::$levelMapping[$level];
211 }
212
213 if ( $channel === 'wfLogDBError' ) {
214 // wfLogDBError messages are emitted if a database log location is
215 // specified.
216 $shouldEmit = (bool)$wgDBerrorLog;
217
218 } elseif ( $channel === 'wfDebug' ) {
219 // wfDebug messages are emitted if a catch all logging file has
220 // been specified. Checked explicitly so that 'private' flagged
221 // messages are not discarded by unset $wgDebugLogGroups channel
222 // handling below.
223 $shouldEmit = $wgDebugLogFile != '';
224
225 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
226 $logConfig = $wgDebugLogGroups[$channel];
227
228 if ( is_array( $logConfig ) ) {
229 $shouldEmit = true;
230 if ( isset( $logConfig['sample'] ) ) {
231 // Emit randomly with a 1 in 'sample' chance for each message.
232 $shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
233 }
234
235 if ( isset( $logConfig['level'] ) ) {
236 $shouldEmit = $level >= self::$levelMapping[$logConfig['level']];
237 }
238 } else {
239 // Emit unless the config value is explicitly false.
240 $shouldEmit = $logConfig !== false;
241 }
242
243 } elseif ( isset( $context['private'] ) && $context['private'] ) {
244 // Don't emit if the message didn't match previous checks based on
245 // the channel and the event is marked as private. This check
246 // discards messages sent via wfDebugLog() with dest == 'private'
247 // and no explicit wgDebugLogGroups configuration.
248 $shouldEmit = false;
249 } else {
250 // Default return value is the same as the historic wfDebug
251 // method: emit if $wgDebugLogFile has been set.
252 $shouldEmit = $wgDebugLogFile != '';
253 }
254
255 return $shouldEmit;
256 }
257
270 public static function format( $channel, $message, $context ) {
272
273 if ( $channel === 'wfDebug' ) {
274 $text = self::formatAsWfDebug( $channel, $message, $context );
275
276 } elseif ( $channel === 'wfLogDBError' ) {
277 $text = self::formatAsWfLogDBError( $channel, $message, $context );
278
279 } elseif ( !isset( $wgDebugLogGroups[$channel] ) ) {
280 $text = self::formatAsWfDebug(
281 $channel, "[{$channel}] {$message}", $context );
282
283 } else {
284 // Default formatting is wfDebugLog's historic style
285 $text = self::formatAsWfDebugLog( $channel, $message, $context );
286 }
287
288 // Append stacktrace of throwable if available
289 if ( $wgLogExceptionBacktrace && isset( $context['exception'] ) ) {
290 $e = $context['exception'];
291 $backtrace = false;
292
293 if ( $e instanceof Throwable ) {
294 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
295
296 } elseif ( is_array( $e ) && isset( $e['trace'] ) ) {
297 // Throwable has already been unpacked as structured data
298 $backtrace = $e['trace'];
299 }
300
301 if ( $backtrace ) {
302 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
303 "\n";
304 }
305 }
306
307 return self::interpolate( $text, $context );
308 }
309
318 protected static function formatAsWfDebug( $channel, $message, $context ) {
319 $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $message );
320 if ( isset( $context['seconds_elapsed'] ) ) {
321 // Prepend elapsed request time and real memory usage with two
322 // trailing spaces.
323 $text = "{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
324 }
325 if ( isset( $context['prefix'] ) ) {
326 $text = "{$context['prefix']}{$text}";
327 }
328 return "{$text}\n";
329 }
330
339 protected static function formatAsWfLogDBError( $channel, $message, $context ) {
340 global $wgDBerrorLogTZ;
341 static $cachedTimezone = null;
342
343 if ( !$cachedTimezone ) {
344 $cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ );
345 }
346
347 $d = date_create( 'now', $cachedTimezone );
348 $date = $d->format( 'D M j G:i:s T Y' );
349
350 $host = wfHostname();
351 $wiki = WikiMap::getCurrentWikiId();
352
353 $text = "{$date}\t{$host}\t{$wiki}\t{$message}\n";
354 return $text;
355 }
356
365 protected static function formatAsWfDebugLog( $channel, $message, $context ) {
366 $time = ConvertibleTimestamp::now( TS::DB );
367 $wiki = WikiMap::getCurrentWikiId();
368 $host = wfHostname();
369 $text = "{$time} {$host} {$wiki}: {$message}\n";
370 return $text;
371 }
372
380 public static function interpolate( $message, array $context ) {
381 if ( str_contains( $message, '{' ) ) {
382 $replace = [];
383 foreach ( $context as $key => $val ) {
384 $replace['{' . $key . '}'] = self::flatten( $val );
385 }
386 $message = strtr( $message, $replace );
387 }
388 return $message;
389 }
390
398 protected static function flatten( $item ) {
399 if ( $item === null ) {
400 return '[Null]';
401 }
402
403 if ( is_bool( $item ) ) {
404 return $item ? 'true' : 'false';
405 }
406
407 if ( is_float( $item ) ) {
408 if ( is_infinite( $item ) ) {
409 return ( $item > 0 ? '' : '-' ) . 'INF';
410 }
411 if ( is_nan( $item ) ) {
412 return 'NaN';
413 }
414 return (string)$item;
415 }
416
417 if ( is_scalar( $item ) ) {
418 return (string)$item;
419 }
420
421 if ( is_array( $item ) ) {
422 return '[Array(' . count( $item ) . ')]';
423 }
424
425 if ( $item instanceof \DateTime ) {
426 return $item->format( 'c' );
427 }
428
429 if ( $item instanceof Throwable ) {
430 $which = $item instanceof Error ? 'Error' : 'Exception';
431 return '[' . $which . ' ' . get_class( $item ) . '( ' .
432 $item->getFile() . ':' . $item->getLine() . ') ' .
433 $item->getMessage() . ']';
434 }
435
436 if ( is_object( $item ) ) {
437 if ( method_exists( $item, '__toString' ) ) {
438 return (string)$item;
439 }
440
441 return '[Object ' . get_class( $item ) . ']';
442 }
443
444 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
445 if ( is_resource( $item ) ) {
446 return '[Resource ' . get_resource_type( $item ) . ']';
447 }
448
449 return '[Unknown ' . get_debug_type( $item ) . ']';
450 }
451
462 protected static function destination( $channel, $message, $context ) {
464
465 // Default destination is the debug log file as historically used by
466 // the wfDebug function.
467 $destination = $wgDebugLogFile;
468
469 if ( isset( $context['destination'] ) ) {
470 // Use destination explicitly provided in context
471 $destination = $context['destination'];
472
473 } elseif ( $channel === 'wfDebug' ) {
474 $destination = $wgDebugLogFile;
475
476 } elseif ( $channel === 'wfLogDBError' ) {
477 $destination = $wgDBerrorLog;
478
479 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
480 $logConfig = $wgDebugLogGroups[$channel];
481
482 if ( is_array( $logConfig ) ) {
483 $destination = $logConfig['destination'];
484 } else {
485 $destination = strval( $logConfig );
486 }
487 }
488
489 return $destination;
490 }
491
501 public static function emit( $text, $file ) {
502 if ( str_starts_with( $file, 'udp:' ) ) {
503 $transport = UDPTransport::newFromString( $file );
504 $transport->emit( $text );
505 } else {
506 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
507 $exists = @file_exists( $file );
508 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
509 $size = $exists ? @filesize( $file ) : false;
510 if ( !$exists ||
511 ( $size !== false && $size + strlen( $text ) < 0x7fffffff )
512 ) {
513 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
514 @file_put_contents( $file, $text, FILE_APPEND );
515 }
516 }
517 }
518
526 private function maybeLogToStderr( string $text ): void {
527 if ( getenv( 'MW_LOG_STDERR' ) ) {
528 error_log( trim( $text ) );
529 }
530 }
531
532}
wfIsDebugRawPage()
Returns true if debug logging should be suppressed if $wgDebugRawPage = false.
wfHostname()
Get host name of the current machine, for use in error reporting.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Debug toolbar.
Definition MWDebug.php:35
Handler class for MWExceptions.
PSR-3 logger that mimics the historic implementation of MediaWiki's former wfErrorLog logging impleme...
static flatten( $item)
Convert a logging context element to a string suitable for interpolation.
static formatAsWfDebugLog( $channel, $message, $context)
Format a message as `wfDebugLog() would have formatted it.
static shouldEmit( $channel, $message, $level, $context)
Determine if the given message should be emitted or not.
log( $level, $message, array $context=[])
Logs with an arbitrary level.
static formatAsWfLogDBError( $channel, $message, $context)
Format a message as wfLogDBError() would have formatted it.
static interpolate( $message, array $context)
Interpolate placeholders in logging message.
static destination( $channel, $message, $context)
Select the appropriate log output destination for the given log event.
setMinimumForTest(?int $level)
Change an existing Logger singleton to act like NullLogger.
static emit( $text, $file)
Log to a file without getting "file size exceeded" signals.
static format( $channel, $message, $context)
Format a message.
static formatAsWfDebug( $channel, $message, $context)
Format a message as wfDebug() would have formatted it.
static array $levelMapping
Convert \Psr\Log\LogLevel constants into int for sensible comparisons These are the same values that ...
static getContext()
Get a logging context, which can be used to add information to all log events.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
A generic class to send a message over UDP.
$wgLogExceptionBacktrace
Config variable stub for the LogExceptionBacktrace setting, for use by phpdoc and IDEs.
$wgDBerrorLogTZ
Config variable stub for the DBerrorLogTZ setting, for use by phpdoc and IDEs.
$wgDBerrorLog
Config variable stub for the DBerrorLog setting, for use by phpdoc and IDEs.
$wgDebugRawPage
Config variable stub for the DebugRawPage setting, for use by phpdoc and IDEs.
$wgShowDebug
Config variable stub for the ShowDebug setting, for use by phpdoc and IDEs.
$wgDebugToolbar
Config variable stub for the DebugToolbar setting, for use by phpdoc and IDEs.
$wgDebugLogGroups
Config variable stub for the DebugLogGroups setting, for use by phpdoc and IDEs.
$wgDebugLogFile
Config variable stub for the DebugLogFile setting, for use by phpdoc and IDEs.