MediaWiki 1.42.0
LegacyLogger.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Logger;
22
23use DateTimeZone;
24use Error;
25use LogicException;
27use MWDebug;
29use Psr\Log\AbstractLogger;
30use Psr\Log\LogLevel;
31use Throwable;
32use UDPTransport;
33use Wikimedia\AtEase\AtEase;
34
52class LegacyLogger extends AbstractLogger {
53
57 protected $channel;
58
59 private const LEVEL_DEBUG = 100;
60 private const LEVEL_INFO = 200;
61 private const LEVEL_NOTICE = 250;
62 private const LEVEL_WARNING = 300;
63 private const LEVEL_ERROR = 400;
64 private const LEVEL_CRITICAL = 500;
65 private const LEVEL_ALERT = 550;
66 private const LEVEL_EMERGENCY = 600;
67 private const LEVEL_INFINITY = 999;
68
75 protected static $levelMapping = [
76 LogLevel::DEBUG => self::LEVEL_DEBUG,
77 LogLevel::INFO => self::LEVEL_INFO,
78 LogLevel::NOTICE => self::LEVEL_NOTICE,
79 LogLevel::WARNING => self::LEVEL_WARNING,
80 LogLevel::ERROR => self::LEVEL_ERROR,
81 LogLevel::CRITICAL => self::LEVEL_CRITICAL,
82 LogLevel::ALERT => self::LEVEL_ALERT,
83 LogLevel::EMERGENCY => self::LEVEL_EMERGENCY,
84 ];
85
92 private $minimumLevel;
93
99 private $isDB;
100
104 public function __construct( $channel ) {
106
107 $this->channel = $channel;
108 $this->isDB = ( $channel === 'rdbms' );
109
110 // Calculate minimum level, duplicating some of the logic from log() and shouldEmit()
111 if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
112 $this->minimumLevel = self::LEVEL_WARNING;
113 } elseif ( $wgDebugLogFile != '' || $wgShowDebug || $wgDebugToolbar ) {
114 // Log all messages if there is a debug log file or debug toolbar
115 $this->minimumLevel = self::LEVEL_DEBUG;
116 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
117 $logConfig = $wgDebugLogGroups[$channel];
118 // Log messages if the config is set, according to the configured level
119 if ( is_array( $logConfig ) && isset( $logConfig['level'] ) ) {
120 $this->minimumLevel = self::$levelMapping[$logConfig['level']];
121 } else {
122 $this->minimumLevel = self::LEVEL_DEBUG;
123 }
124 } else {
125 // No other case hit: discard all messages
126 $this->minimumLevel = self::LEVEL_INFINITY;
127 }
128
129 if ( $this->isDB && $wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
130 // Log DB errors if there is a DB error log
131 $this->minimumLevel = self::LEVEL_ERROR;
132 }
133 }
134
142 public function setMinimumForTest( ?int $level ) {
143 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
144 throw new LogicException( 'Not allowed outside tests' );
145 }
146 // Set LEVEL_INFINITY if given null, or restore the original level.
147 $original = $this->minimumLevel;
148 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
149 return $original;
150 }
151
159 public function log( $level, $message, array $context = [] ) {
160 if ( is_string( $level ) ) {
161 $level = self::$levelMapping[$level];
162 }
163 if ( $level < $this->minimumLevel ) {
164 return;
165 }
166
167 if ( $this->isDB
168 && $level === self::LEVEL_DEBUG
169 && isset( $context['sql'] )
170 ) {
171 // Also give the query information to the MWDebug tools
173 $context['sql'],
174 $context['method'],
175 $context['runtime_ms'] / 1000,
176 $context['db_server']
177 );
178 }
179
180 // If this is a DB-related error, and the site has $wgDBerrorLog
181 // configured, rewrite the channel as wfLogDBError instead.
182 // Likewise, if the site does not use $wgDBerrorLog, it should
183 // configurable like any other channel via $wgDebugLogGroups
184 // or $wgMWLoggerDefaultSpi.
185 global $wgDBerrorLog;
186 if ( $this->isDB && $level >= self::LEVEL_ERROR && $wgDBerrorLog ) {
187 // Format and write DB errors to the legacy locations
188 $effectiveChannel = 'wfLogDBError';
189 } else {
190 $effectiveChannel = $this->channel;
191 }
192
193 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
194 $text = self::format( $effectiveChannel, $message, $context );
195 $destination = self::destination( $effectiveChannel, $message, $context );
196 $this->maybeLogToStderr( $text );
197 self::emit( $text, $destination );
198 }
199 if ( !isset( $context['private'] ) || !$context['private'] ) {
200 // Add to debug toolbar if not marked as "private"
201 MWDebug::debugMsg( $message, [ 'channel' => $this->channel ] + $context );
202 }
203 }
204
215 public static function shouldEmit( $channel, $message, $level, $context ) {
217
218 if ( is_string( $level ) ) {
219 $level = self::$levelMapping[$level];
220 }
221
222 if ( $channel === 'wfLogDBError' ) {
223 // wfLogDBError messages are emitted if a database log location is
224 // specified.
225 $shouldEmit = (bool)$wgDBerrorLog;
226
227 } elseif ( $channel === 'wfDebug' ) {
228 // wfDebug messages are emitted if a catch all logging file has
229 // been specified. Checked explicitly so that 'private' flagged
230 // messages are not discarded by unset $wgDebugLogGroups channel
231 // handling below.
232 $shouldEmit = $wgDebugLogFile != '';
233
234 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
235 $logConfig = $wgDebugLogGroups[$channel];
236
237 if ( is_array( $logConfig ) ) {
238 $shouldEmit = true;
239 if ( isset( $logConfig['sample'] ) ) {
240 // Emit randomly with a 1 in 'sample' chance for each message.
241 $shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
242 }
243
244 if ( isset( $logConfig['level'] ) ) {
245 $shouldEmit = $level >= self::$levelMapping[$logConfig['level']];
246 }
247 } else {
248 // Emit unless the config value is explicitly false.
249 $shouldEmit = $logConfig !== false;
250 }
251
252 } elseif ( isset( $context['private'] ) && $context['private'] ) {
253 // Don't emit if the message didn't match previous checks based on
254 // the channel and the event is marked as private. This check
255 // discards messages sent via wfDebugLog() with dest == 'private'
256 // and no explicit wgDebugLogGroups configuration.
257 $shouldEmit = false;
258 } else {
259 // Default return value is the same as the historic wfDebug
260 // method: emit if $wgDebugLogFile has been set.
261 $shouldEmit = $wgDebugLogFile != '';
262 }
263
264 return $shouldEmit;
265 }
266
279 public static function format( $channel, $message, $context ) {
281
282 if ( $channel === 'wfDebug' ) {
283 $text = self::formatAsWfDebug( $channel, $message, $context );
284
285 } elseif ( $channel === 'wfLogDBError' ) {
286 $text = self::formatAsWfLogDBError( $channel, $message, $context );
287
288 } elseif ( !isset( $wgDebugLogGroups[$channel] ) ) {
289 $text = self::formatAsWfDebug(
290 $channel, "[{$channel}] {$message}", $context );
291
292 } else {
293 // Default formatting is wfDebugLog's historic style
294 $text = self::formatAsWfDebugLog( $channel, $message, $context );
295 }
296
297 // Append stacktrace of throwable if available
298 if ( $wgLogExceptionBacktrace && isset( $context['exception'] ) ) {
299 $e = $context['exception'];
300 $backtrace = false;
301
302 if ( $e instanceof Throwable ) {
303 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
304
305 } elseif ( is_array( $e ) && isset( $e['trace'] ) ) {
306 // Throwable has already been unpacked as structured data
307 $backtrace = $e['trace'];
308 }
309
310 if ( $backtrace ) {
311 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
312 "\n";
313 }
314 }
315
316 return self::interpolate( $text, $context );
317 }
318
327 protected static function formatAsWfDebug( $channel, $message, $context ) {
328 $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $message );
329 if ( isset( $context['seconds_elapsed'] ) ) {
330 // Prepend elapsed request time and real memory usage with two
331 // trailing spaces.
332 $text = "{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
333 }
334 if ( isset( $context['prefix'] ) ) {
335 $text = "{$context['prefix']}{$text}";
336 }
337 return "{$text}\n";
338 }
339
348 protected static function formatAsWfLogDBError( $channel, $message, $context ) {
349 global $wgDBerrorLogTZ;
350 static $cachedTimezone = null;
351
352 if ( !$cachedTimezone ) {
353 $cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ );
354 }
355
356 $d = date_create( 'now', $cachedTimezone );
357 $date = $d->format( 'D M j G:i:s T Y' );
358
359 $host = wfHostname();
360 $wiki = WikiMap::getCurrentWikiId();
361
362 $text = "{$date}\t{$host}\t{$wiki}\t{$message}\n";
363 return $text;
364 }
365
374 protected static function formatAsWfDebugLog( $channel, $message, $context ) {
375 $time = wfTimestamp( TS_DB );
376 $wiki = WikiMap::getCurrentWikiId();
377 $host = wfHostname();
378 $text = "{$time} {$host} {$wiki}: {$message}\n";
379 return $text;
380 }
381
389 public static function interpolate( $message, array $context ) {
390 if ( str_contains( $message, '{' ) ) {
391 $replace = [];
392 foreach ( $context as $key => $val ) {
393 $replace['{' . $key . '}'] = self::flatten( $val );
394 }
395 $message = strtr( $message, $replace );
396 }
397 return $message;
398 }
399
407 protected static function flatten( $item ) {
408 if ( $item === null ) {
409 return '[Null]';
410 }
411
412 if ( is_bool( $item ) ) {
413 return $item ? 'true' : 'false';
414 }
415
416 if ( is_float( $item ) ) {
417 if ( is_infinite( $item ) ) {
418 return ( $item > 0 ? '' : '-' ) . 'INF';
419 }
420 if ( is_nan( $item ) ) {
421 return 'NaN';
422 }
423 return (string)$item;
424 }
425
426 if ( is_scalar( $item ) ) {
427 return (string)$item;
428 }
429
430 if ( is_array( $item ) ) {
431 return '[Array(' . count( $item ) . ')]';
432 }
433
434 if ( $item instanceof \DateTime ) {
435 return $item->format( 'c' );
436 }
437
438 if ( $item instanceof Throwable ) {
439 $which = $item instanceof Error ? 'Error' : 'Exception';
440 return '[' . $which . ' ' . get_class( $item ) . '( ' .
441 $item->getFile() . ':' . $item->getLine() . ') ' .
442 $item->getMessage() . ']';
443 }
444
445 if ( is_object( $item ) ) {
446 if ( method_exists( $item, '__toString' ) ) {
447 return (string)$item;
448 }
449
450 return '[Object ' . get_class( $item ) . ']';
451 }
452
453 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
454 if ( is_resource( $item ) ) {
455 return '[Resource ' . get_resource_type( $item ) . ']';
456 }
457
458 return '[Unknown ' . gettype( $item ) . ']';
459 }
460
471 protected static function destination( $channel, $message, $context ) {
473
474 // Default destination is the debug log file as historically used by
475 // the wfDebug function.
476 $destination = $wgDebugLogFile;
477
478 if ( isset( $context['destination'] ) ) {
479 // Use destination explicitly provided in context
480 $destination = $context['destination'];
481
482 } elseif ( $channel === 'wfDebug' ) {
483 $destination = $wgDebugLogFile;
484
485 } elseif ( $channel === 'wfLogDBError' ) {
486 $destination = $wgDBerrorLog;
487
488 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
489 $logConfig = $wgDebugLogGroups[$channel];
490
491 if ( is_array( $logConfig ) ) {
492 $destination = $logConfig['destination'];
493 } else {
494 $destination = strval( $logConfig );
495 }
496 }
497
498 return $destination;
499 }
500
510 public static function emit( $text, $file ) {
511 if ( str_starts_with( $file, 'udp:' ) ) {
512 $transport = UDPTransport::newFromString( $file );
513 $transport->emit( $text );
514 } else {
515 AtEase::suppressWarnings();
516 $exists = file_exists( $file );
517 $size = $exists ? filesize( $file ) : false;
518 if ( !$exists ||
519 ( $size !== false && $size + strlen( $text ) < 0x7fffffff )
520 ) {
521 file_put_contents( $file, $text, FILE_APPEND );
522 }
523 AtEase::restoreWarnings();
524 }
525 }
526
534 private function maybeLogToStderr( string $text ): void {
535 if ( getenv( 'MW_LOG_STDERR' ) ) {
536 error_log( trim( $text ) );
537 }
538 }
539
540}
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.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Debug toolbar.
Definition MWDebug.php:42
static query( $sql, $function, $runTime, $dbhost)
Begins profiling on a database query.
Definition MWDebug.php:572
static debugMsg( $str, $context=[])
This method receives messages from LoggerFactory, wfDebugLog, and MWExceptionHandler.
Definition MWDebug.php:508
Handler class for MWExceptions.
static getRedactedTrace(Throwable $e)
Return a copy of a throwable's backtrace as an array.
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
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 ...
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
A generic class to send a message over UDP.
static newFromString( $info)
$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.