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 = [] ) {
160 if ( is_string( $level ) ) {
161 $level = self::$levelMapping[$level];
163 if ( $level < $this->minimumLevel ) {
168 && $level === self::LEVEL_DEBUG
169 && isset( $context[
'sql'] )
175 $context[
'runtime_ms'] / 1000,
176 $context[
'db_server']
186 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
188 $effectiveChannel =
'wfLogDBError';
193 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
194 $text =
self::format( $effectiveChannel, $message, $context );
196 $this->maybeLogToStderr( $text );
199 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
201 MWDebug::debugMsg( $message, [
'channel' => $this->channel ] + $context );
218 if ( is_string( $level ) ) {
219 $level = self::$levelMapping[$level];
222 if (
$channel ===
'wfLogDBError' ) {
227 } elseif (
$channel ===
'wfDebug' ) {
237 if ( is_array( $logConfig ) ) {
239 if ( isset( $logConfig[
'sample'] ) ) {
241 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
244 if ( isset( $logConfig[
'level'] ) ) {
245 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
249 $shouldEmit = $logConfig !==
false;
252 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
285 } elseif (
$channel ===
'wfLogDBError' ) {
290 $channel,
"[{$channel}] {$message}", $context );
299 $e = $context[
'exception'];
302 if ( $e instanceof Throwable ) {
303 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
305 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
307 $backtrace = $e[
'trace'];
311 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
328 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
329 if ( isset( $context[
'seconds_elapsed'] ) ) {
332 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
334 if ( isset( $context[
'prefix'] ) ) {
335 $text =
"{$context['prefix']}{$text}";
350 static $cachedTimezone =
null;
352 if ( !$cachedTimezone ) {
356 $d = date_create(
'now', $cachedTimezone );
357 $date = $d->format(
'D M j G:i:s T Y' );
360 $wiki = WikiMap::getCurrentWikiId();
362 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
376 $wiki = WikiMap::getCurrentWikiId();
378 $text =
"{$time} {$host} {$wiki}: {$message}\n";
390 if ( str_contains( $message,
'{' ) ) {
392 foreach ( $context as $key => $val ) {
395 $message = strtr( $message, $replace );
408 if ( $item ===
null ) {
412 if ( is_bool( $item ) ) {
413 return $item ?
'true' :
'false';
416 if ( is_float( $item ) ) {
417 if ( is_infinite( $item ) ) {
418 return ( $item > 0 ?
'' :
'-' ) .
'INF';
420 if ( is_nan( $item ) ) {
423 return (
string)$item;
426 if ( is_scalar( $item ) ) {
427 return (
string)$item;
430 if ( is_array( $item ) ) {
431 return '[Array(' . count( $item ) .
')]';
434 if ( $item instanceof \DateTime ) {
435 return $item->format(
'c' );
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() .
']';
445 if ( is_object( $item ) ) {
446 if ( method_exists( $item,
'__toString' ) ) {
447 return (
string)$item;
450 return '[Object ' . get_class( $item ) .
']';
454 if ( is_resource( $item ) ) {
455 return '[Resource ' . get_resource_type( $item ) .
']';
458 return '[Unknown ' . get_debug_type( $item ) .
']';
478 if ( isset( $context[
'destination'] ) ) {
480 $destination = $context[
'destination'];
482 } elseif (
$channel ===
'wfDebug' ) {
485 } elseif (
$channel ===
'wfLogDBError' ) {
491 if ( is_array( $logConfig ) ) {
492 $destination = $logConfig[
'destination'];
494 $destination = strval( $logConfig );
510 public static function emit( $text, $file ) {
511 if ( str_starts_with( $file,
'udp:' ) ) {
513 $transport->emit( $text );
515 AtEase::suppressWarnings();
516 $exists = file_exists( $file );
517 $size = $exists ? filesize( $file ) :
false;
519 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
521 file_put_contents( $file, $text, FILE_APPEND );
523 AtEase::restoreWarnings();
534 private function maybeLogToStderr(
string $text ): void {
535 if ( getenv(
'MW_LOG_STDERR' ) ) {
536 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'))
Handler class for MWExceptions.
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.