16use Psr\Log\AbstractLogger;
19use Wikimedia\Timestamp\ConvertibleTimestamp;
20use Wikimedia\Timestamp\TimestampFormat as TS;
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;
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,
80 private $minimumLevel;
96 $this->isDB = (
$channel ===
'rdbms' );
100 $this->minimumLevel = self::LEVEL_WARNING;
103 $this->minimumLevel = self::LEVEL_DEBUG;
107 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
108 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
110 $this->minimumLevel = self::LEVEL_DEBUG;
114 $this->minimumLevel = self::LEVEL_INFINITY;
117 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
119 $this->minimumLevel = self::LEVEL_ERROR;
131 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
132 throw new LogicException(
'Not allowed outside tests' );
135 $original = $this->minimumLevel;
136 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
147 public function log( $level, $message, array $context = [] ): void {
148 if ( is_string( $level ) ) {
149 $level = self::$levelMapping[$level];
151 if ( $level < $this->minimumLevel ) {
158 && $level === self::LEVEL_DEBUG
159 && isset( $context[
'sql'] )
165 $context[
'runtime_ms'] / 1000,
166 $context[
'db_server'],
177 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
179 $effectiveChannel =
'wfLogDBError';
184 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
185 $text =
self::format( $effectiveChannel, $message, $context );
187 $this->maybeLogToStderr( $text );
190 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
192 MWDebug::debugMsg( $message, [
'channel' => $this->channel ] + $context );
206 public static function shouldEmit( $channel, $message, $level, $context ) {
209 if ( is_string( $level ) ) {
210 $level = self::$levelMapping[$level];
213 if ( $channel ===
'wfLogDBError' ) {
218 } elseif ( $channel ===
'wfDebug' ) {
228 if ( is_array( $logConfig ) ) {
230 if ( isset( $logConfig[
'sample'] ) ) {
232 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
235 if ( isset( $logConfig[
'level'] ) ) {
236 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
240 $shouldEmit = $logConfig !==
false;
243 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
270 public static function format( $channel, $message, $context ) {
273 if ( $channel ===
'wfDebug' ) {
274 $text = self::formatAsWfDebug( $channel, $message, $context );
276 } elseif ( $channel ===
'wfLogDBError' ) {
277 $text = self::formatAsWfLogDBError( $channel, $message, $context );
280 $text = self::formatAsWfDebug(
281 $channel,
"[{$channel}] {$message}", $context );
285 $text = self::formatAsWfDebugLog( $channel, $message, $context );
290 $e = $context[
'exception'];
293 if ( $e instanceof Throwable ) {
294 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
296 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
298 $backtrace = $e[
'trace'];
302 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
307 return self::interpolate( $text, $context );
319 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
320 if ( isset( $context[
'seconds_elapsed'] ) ) {
323 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
325 if ( isset( $context[
'prefix'] ) ) {
326 $text =
"{$context['prefix']}{$text}";
341 static $cachedTimezone =
null;
343 if ( !$cachedTimezone ) {
347 $d = date_create(
'now', $cachedTimezone );
348 $date = $d->format(
'D M j G:i:s T Y' );
351 $wiki = WikiMap::getCurrentWikiId();
353 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
366 $time = ConvertibleTimestamp::now( TS::DB );
367 $wiki = WikiMap::getCurrentWikiId();
369 $text =
"{$time} {$host} {$wiki}: {$message}\n";
381 if ( str_contains( $message,
'{' ) ) {
383 foreach ( $context as $key => $val ) {
384 $replace[
'{' . $key .
'}'] = self::flatten( $val );
386 $message = strtr( $message, $replace );
399 if ( $item ===
null ) {
403 if ( is_bool( $item ) ) {
404 return $item ?
'true' :
'false';
407 if ( is_float( $item ) ) {
408 if ( is_infinite( $item ) ) {
409 return ( $item > 0 ?
'' :
'-' ) .
'INF';
411 if ( is_nan( $item ) ) {
414 return (
string)$item;
417 if ( is_scalar( $item ) ) {
418 return (
string)$item;
421 if ( is_array( $item ) ) {
422 return '[Array(' . count( $item ) .
')]';
425 if ( $item instanceof \DateTime ) {
426 return $item->format(
'c' );
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() .
']';
436 if ( is_object( $item ) ) {
437 if ( method_exists( $item,
'__toString' ) ) {
438 return (
string)$item;
441 return '[Object ' . get_class( $item ) .
']';
445 if ( is_resource( $item ) ) {
446 return '[Resource ' . get_resource_type( $item ) .
']';
449 return '[Unknown ' . get_debug_type( $item ) .
']';
462 protected static function destination( $channel, $message, $context ) {
469 if ( isset( $context[
'destination'] ) ) {
471 $destination = $context[
'destination'];
473 } elseif ( $channel ===
'wfDebug' ) {
476 } elseif ( $channel ===
'wfLogDBError' ) {
482 if ( is_array( $logConfig ) ) {
483 $destination = $logConfig[
'destination'];
485 $destination = strval( $logConfig );
501 public static function emit( $text, $file ) {
502 if ( str_starts_with( $file,
'udp:' ) ) {
503 $transport = UDPTransport::newFromString( $file );
504 $transport->emit( $text );
507 $exists = @file_exists( $file );
509 $size = $exists ? @filesize( $file ) :
false;
511 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
514 @file_put_contents( $file, $text, FILE_APPEND );
526 private function maybeLogToStderr(
string $text ): void {
527 if ( getenv(
'MW_LOG_STDERR' ) ) {
528 error_log( trim( $text ) );
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'))
$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.