15use Psr\Log\AbstractLogger;
19use Wikimedia\AtEase\AtEase;
45 private const LEVEL_DEBUG = 100;
46 private const LEVEL_INFO = 200;
47 private const LEVEL_NOTICE = 250;
48 private const LEVEL_WARNING = 300;
49 private const LEVEL_ERROR = 400;
50 private const LEVEL_CRITICAL = 500;
51 private const LEVEL_ALERT = 550;
52 private const LEVEL_EMERGENCY = 600;
53 private const LEVEL_INFINITY = 999;
62 LogLevel::DEBUG => self::LEVEL_DEBUG,
63 LogLevel::INFO => self::LEVEL_INFO,
64 LogLevel::NOTICE => self::LEVEL_NOTICE,
65 LogLevel::WARNING => self::LEVEL_WARNING,
66 LogLevel::ERROR => self::LEVEL_ERROR,
67 LogLevel::CRITICAL => self::LEVEL_CRITICAL,
68 LogLevel::ALERT => self::LEVEL_ALERT,
69 LogLevel::EMERGENCY => self::LEVEL_EMERGENCY,
78 private $minimumLevel;
94 $this->isDB = (
$channel ===
'rdbms' );
98 $this->minimumLevel = self::LEVEL_WARNING;
101 $this->minimumLevel = self::LEVEL_DEBUG;
105 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
106 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
108 $this->minimumLevel = self::LEVEL_DEBUG;
112 $this->minimumLevel = self::LEVEL_INFINITY;
115 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
117 $this->minimumLevel = self::LEVEL_ERROR;
129 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
130 throw new LogicException(
'Not allowed outside tests' );
133 $original = $this->minimumLevel;
134 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
145 public function log( $level, $message, array $context = [] ): void {
146 if ( is_string( $level ) ) {
147 $level = self::$levelMapping[$level];
149 if ( $level < $this->minimumLevel ) {
156 && $level === self::LEVEL_DEBUG
157 && isset( $context[
'sql'] )
163 $context[
'runtime_ms'] / 1000,
164 $context[
'db_server']
174 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
176 $effectiveChannel =
'wfLogDBError';
181 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
182 $text =
self::format( $effectiveChannel, $message, $context );
184 $this->maybeLogToStderr( $text );
187 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
189 MWDebug::debugMsg( $message, [
'channel' => $this->channel ] + $context );
203 public static function shouldEmit( $channel, $message, $level, $context ) {
206 if ( is_string( $level ) ) {
207 $level = self::$levelMapping[$level];
210 if ( $channel ===
'wfLogDBError' ) {
215 } elseif ( $channel ===
'wfDebug' ) {
225 if ( is_array( $logConfig ) ) {
227 if ( isset( $logConfig[
'sample'] ) ) {
229 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
232 if ( isset( $logConfig[
'level'] ) ) {
233 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
237 $shouldEmit = $logConfig !==
false;
240 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
267 public static function format( $channel, $message, $context ) {
270 if ( $channel ===
'wfDebug' ) {
271 $text = self::formatAsWfDebug( $channel, $message, $context );
273 } elseif ( $channel ===
'wfLogDBError' ) {
274 $text = self::formatAsWfLogDBError( $channel, $message, $context );
277 $text = self::formatAsWfDebug(
278 $channel,
"[{$channel}] {$message}", $context );
282 $text = self::formatAsWfDebugLog( $channel, $message, $context );
287 $e = $context[
'exception'];
290 if ( $e instanceof Throwable ) {
291 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
293 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
295 $backtrace = $e[
'trace'];
299 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
304 return self::interpolate( $text, $context );
316 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
317 if ( isset( $context[
'seconds_elapsed'] ) ) {
320 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
322 if ( isset( $context[
'prefix'] ) ) {
323 $text =
"{$context['prefix']}{$text}";
338 static $cachedTimezone =
null;
340 if ( !$cachedTimezone ) {
344 $d = date_create(
'now', $cachedTimezone );
345 $date = $d->format(
'D M j G:i:s T Y' );
348 $wiki = WikiMap::getCurrentWikiId();
350 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
364 $wiki = WikiMap::getCurrentWikiId();
366 $text =
"{$time} {$host} {$wiki}: {$message}\n";
378 if ( str_contains( $message,
'{' ) ) {
380 foreach ( $context as $key => $val ) {
381 $replace[
'{' . $key .
'}'] = self::flatten( $val );
383 $message = strtr( $message, $replace );
396 if ( $item ===
null ) {
400 if ( is_bool( $item ) ) {
401 return $item ?
'true' :
'false';
404 if ( is_float( $item ) ) {
405 if ( is_infinite( $item ) ) {
406 return ( $item > 0 ?
'' :
'-' ) .
'INF';
408 if ( is_nan( $item ) ) {
411 return (
string)$item;
414 if ( is_scalar( $item ) ) {
415 return (
string)$item;
418 if ( is_array( $item ) ) {
419 return '[Array(' . count( $item ) .
')]';
422 if ( $item instanceof \DateTime ) {
423 return $item->format(
'c' );
426 if ( $item instanceof Throwable ) {
427 $which = $item instanceof Error ?
'Error' :
'Exception';
428 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
429 $item->getFile() .
':' . $item->getLine() .
') ' .
430 $item->getMessage() .
']';
433 if ( is_object( $item ) ) {
434 if ( method_exists( $item,
'__toString' ) ) {
435 return (
string)$item;
438 return '[Object ' . get_class( $item ) .
']';
442 if ( is_resource( $item ) ) {
443 return '[Resource ' . get_resource_type( $item ) .
']';
446 return '[Unknown ' . get_debug_type( $item ) .
']';
459 protected static function destination( $channel, $message, $context ) {
466 if ( isset( $context[
'destination'] ) ) {
468 $destination = $context[
'destination'];
470 } elseif ( $channel ===
'wfDebug' ) {
473 } elseif ( $channel ===
'wfLogDBError' ) {
479 if ( is_array( $logConfig ) ) {
480 $destination = $logConfig[
'destination'];
482 $destination = strval( $logConfig );
498 public static function emit( $text, $file ) {
499 if ( str_starts_with( $file,
'udp:' ) ) {
501 $transport->emit( $text );
503 AtEase::suppressWarnings();
504 $exists = file_exists( $file );
505 $size = $exists ? filesize( $file ) :
false;
507 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
509 file_put_contents( $file, $text, FILE_APPEND );
511 AtEase::restoreWarnings();
522 private function maybeLogToStderr(
string $text ): void {
523 if ( getenv(
'MW_LOG_STDERR' ) ) {
524 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.