29use Psr\Log\AbstractLogger;
33use Wikimedia\AtEase\AtEase;
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;
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,
92 private $minimumLevel;
108 $this->isDB = (
$channel ===
'rdbms' );
112 $this->minimumLevel = self::LEVEL_WARNING;
115 $this->minimumLevel = self::LEVEL_DEBUG;
119 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
120 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
122 $this->minimumLevel = self::LEVEL_DEBUG;
126 $this->minimumLevel = self::LEVEL_INFINITY;
129 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
131 $this->minimumLevel = self::LEVEL_ERROR;
143 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
144 throw new LogicException(
'Not allowed outside tests' );
147 $original = $this->minimumLevel;
148 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
159 public function log( $level, $message, array $context = [] ): void {
160 if ( is_string( $level ) ) {
161 $level = self::$levelMapping[$level];
163 if ( $level < $this->minimumLevel ) {
170 && $level === self::LEVEL_DEBUG
171 && isset( $context[
'sql'] )
177 $context[
'runtime_ms'] / 1000,
178 $context[
'db_server']
188 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
190 $effectiveChannel =
'wfLogDBError';
195 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
196 $text =
self::format( $effectiveChannel, $message, $context );
198 $this->maybeLogToStderr( $text );
201 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
203 MWDebug::debugMsg( $message, [
'channel' => $this->channel ] + $context );
217 public static function shouldEmit( $channel, $message, $level, $context ) {
220 if ( is_string( $level ) ) {
221 $level = self::$levelMapping[$level];
224 if ( $channel ===
'wfLogDBError' ) {
229 } elseif ( $channel ===
'wfDebug' ) {
239 if ( is_array( $logConfig ) ) {
241 if ( isset( $logConfig[
'sample'] ) ) {
243 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
246 if ( isset( $logConfig[
'level'] ) ) {
247 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
251 $shouldEmit = $logConfig !==
false;
254 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
281 public static function format( $channel, $message, $context ) {
284 if ( $channel ===
'wfDebug' ) {
285 $text = self::formatAsWfDebug( $channel, $message, $context );
287 } elseif ( $channel ===
'wfLogDBError' ) {
288 $text = self::formatAsWfLogDBError( $channel, $message, $context );
291 $text = self::formatAsWfDebug(
292 $channel,
"[{$channel}] {$message}", $context );
296 $text = self::formatAsWfDebugLog( $channel, $message, $context );
301 $e = $context[
'exception'];
304 if ( $e instanceof Throwable ) {
305 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
307 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
309 $backtrace = $e[
'trace'];
313 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
318 return self::interpolate( $text, $context );
330 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
331 if ( isset( $context[
'seconds_elapsed'] ) ) {
334 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
336 if ( isset( $context[
'prefix'] ) ) {
337 $text =
"{$context['prefix']}{$text}";
352 static $cachedTimezone =
null;
354 if ( !$cachedTimezone ) {
358 $d = date_create(
'now', $cachedTimezone );
359 $date = $d->format(
'D M j G:i:s T Y' );
362 $wiki = WikiMap::getCurrentWikiId();
364 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
378 $wiki = WikiMap::getCurrentWikiId();
380 $text =
"{$time} {$host} {$wiki}: {$message}\n";
392 if ( str_contains( $message,
'{' ) ) {
394 foreach ( $context as $key => $val ) {
395 $replace[
'{' . $key .
'}'] = self::flatten( $val );
397 $message = strtr( $message, $replace );
410 if ( $item ===
null ) {
414 if ( is_bool( $item ) ) {
415 return $item ?
'true' :
'false';
418 if ( is_float( $item ) ) {
419 if ( is_infinite( $item ) ) {
420 return ( $item > 0 ?
'' :
'-' ) .
'INF';
422 if ( is_nan( $item ) ) {
425 return (
string)$item;
428 if ( is_scalar( $item ) ) {
429 return (
string)$item;
432 if ( is_array( $item ) ) {
433 return '[Array(' . count( $item ) .
')]';
436 if ( $item instanceof \DateTime ) {
437 return $item->format(
'c' );
440 if ( $item instanceof Throwable ) {
441 $which = $item instanceof Error ?
'Error' :
'Exception';
442 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
443 $item->getFile() .
':' . $item->getLine() .
') ' .
444 $item->getMessage() .
']';
447 if ( is_object( $item ) ) {
448 if ( method_exists( $item,
'__toString' ) ) {
449 return (
string)$item;
452 return '[Object ' . get_class( $item ) .
']';
456 if ( is_resource( $item ) ) {
457 return '[Resource ' . get_resource_type( $item ) .
']';
460 return '[Unknown ' . get_debug_type( $item ) .
']';
473 protected static function destination( $channel, $message, $context ) {
480 if ( isset( $context[
'destination'] ) ) {
482 $destination = $context[
'destination'];
484 } elseif ( $channel ===
'wfDebug' ) {
487 } elseif ( $channel ===
'wfLogDBError' ) {
493 if ( is_array( $logConfig ) ) {
494 $destination = $logConfig[
'destination'];
496 $destination = strval( $logConfig );
512 public static function emit( $text, $file ) {
513 if ( str_starts_with( $file,
'udp:' ) ) {
515 $transport->emit( $text );
517 AtEase::suppressWarnings();
518 $exists = file_exists( $file );
519 $size = $exists ? filesize( $file ) :
false;
521 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
523 file_put_contents( $file, $text, FILE_APPEND );
525 AtEase::restoreWarnings();
536 private function maybeLogToStderr(
string $text ): void {
537 if ( getenv(
'MW_LOG_STDERR' ) ) {
538 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.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
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.