15use Psr\Log\AbstractLogger;
19use Wikimedia\Timestamp\ConvertibleTimestamp;
20use Wikimedia\Timestamp\TimestampFormat as TS;
46 private const LEVEL_DEBUG = 100;
47 private const LEVEL_INFO = 200;
48 private const LEVEL_NOTICE = 250;
49 private const LEVEL_WARNING = 300;
50 private const LEVEL_ERROR = 400;
51 private const LEVEL_CRITICAL = 500;
52 private const LEVEL_ALERT = 550;
53 private const LEVEL_EMERGENCY = 600;
54 private const LEVEL_INFINITY = 999;
63 LogLevel::DEBUG => self::LEVEL_DEBUG,
64 LogLevel::INFO => self::LEVEL_INFO,
65 LogLevel::NOTICE => self::LEVEL_NOTICE,
66 LogLevel::WARNING => self::LEVEL_WARNING,
67 LogLevel::ERROR => self::LEVEL_ERROR,
68 LogLevel::CRITICAL => self::LEVEL_CRITICAL,
69 LogLevel::ALERT => self::LEVEL_ALERT,
70 LogLevel::EMERGENCY => self::LEVEL_EMERGENCY,
79 private $minimumLevel;
95 $this->isDB = (
$channel ===
'rdbms' );
99 $this->minimumLevel = self::LEVEL_WARNING;
102 $this->minimumLevel = self::LEVEL_DEBUG;
106 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
107 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
109 $this->minimumLevel = self::LEVEL_DEBUG;
113 $this->minimumLevel = self::LEVEL_INFINITY;
116 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
118 $this->minimumLevel = self::LEVEL_ERROR;
130 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
131 throw new LogicException(
'Not allowed outside tests' );
134 $original = $this->minimumLevel;
135 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
146 public function log( $level, $message, array $context = [] ): void {
147 if ( is_string( $level ) ) {
148 $level = self::$levelMapping[$level];
150 if ( $level < $this->minimumLevel ) {
157 && $level === self::LEVEL_DEBUG
158 && isset( $context[
'sql'] )
164 $context[
'runtime_ms'] / 1000,
165 $context[
'db_server'],
176 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
178 $effectiveChannel =
'wfLogDBError';
183 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
184 $text =
self::format( $effectiveChannel, $message, $context );
186 $this->maybeLogToStderr( $text );
189 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
191 MWDebug::debugMsg( $message, [
'channel' => $this->channel ] + $context );
205 public static function shouldEmit( $channel, $message, $level, $context ) {
208 if ( is_string( $level ) ) {
209 $level = self::$levelMapping[$level];
212 if ( $channel ===
'wfLogDBError' ) {
217 } elseif ( $channel ===
'wfDebug' ) {
227 if ( is_array( $logConfig ) ) {
229 if ( isset( $logConfig[
'sample'] ) ) {
231 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
234 if ( isset( $logConfig[
'level'] ) ) {
235 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
239 $shouldEmit = $logConfig !==
false;
242 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
269 public static function format( $channel, $message, $context ) {
272 if ( $channel ===
'wfDebug' ) {
273 $text = self::formatAsWfDebug( $channel, $message, $context );
275 } elseif ( $channel ===
'wfLogDBError' ) {
276 $text = self::formatAsWfLogDBError( $channel, $message, $context );
279 $text = self::formatAsWfDebug(
280 $channel,
"[{$channel}] {$message}", $context );
284 $text = self::formatAsWfDebugLog( $channel, $message, $context );
289 $e = $context[
'exception'];
292 if ( $e instanceof Throwable ) {
293 $backtrace = MWExceptionHandler::getRedactedTrace( $e );
295 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
297 $backtrace = $e[
'trace'];
301 $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) .
306 return self::interpolate( $text, $context );
318 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
319 if ( isset( $context[
'seconds_elapsed'] ) ) {
322 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
324 if ( isset( $context[
'prefix'] ) ) {
325 $text =
"{$context['prefix']}{$text}";
340 static $cachedTimezone =
null;
342 if ( !$cachedTimezone ) {
346 $d = date_create(
'now', $cachedTimezone );
347 $date = $d->format(
'D M j G:i:s T Y' );
350 $wiki = WikiMap::getCurrentWikiId();
352 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
365 $time = ConvertibleTimestamp::now( TS::DB );
366 $wiki = WikiMap::getCurrentWikiId();
368 $text =
"{$time} {$host} {$wiki}: {$message}\n";
380 if ( str_contains( $message,
'{' ) ) {
382 foreach ( $context as $key => $val ) {
383 $replace[
'{' . $key .
'}'] = self::flatten( $val );
385 $message = strtr( $message, $replace );
398 if ( $item ===
null ) {
402 if ( is_bool( $item ) ) {
403 return $item ?
'true' :
'false';
406 if ( is_float( $item ) ) {
407 if ( is_infinite( $item ) ) {
408 return ( $item > 0 ?
'' :
'-' ) .
'INF';
410 if ( is_nan( $item ) ) {
413 return (
string)$item;
416 if ( is_scalar( $item ) ) {
417 return (
string)$item;
420 if ( is_array( $item ) ) {
421 return '[Array(' . count( $item ) .
')]';
424 if ( $item instanceof \DateTime ) {
425 return $item->format(
'c' );
428 if ( $item instanceof Throwable ) {
429 $which = $item instanceof Error ?
'Error' :
'Exception';
430 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
431 $item->getFile() .
':' . $item->getLine() .
') ' .
432 $item->getMessage() .
']';
435 if ( is_object( $item ) ) {
436 if ( method_exists( $item,
'__toString' ) ) {
437 return (
string)$item;
440 return '[Object ' . get_class( $item ) .
']';
444 if ( is_resource( $item ) ) {
445 return '[Resource ' . get_resource_type( $item ) .
']';
448 return '[Unknown ' . get_debug_type( $item ) .
']';
461 protected static function destination( $channel, $message, $context ) {
468 if ( isset( $context[
'destination'] ) ) {
470 $destination = $context[
'destination'];
472 } elseif ( $channel ===
'wfDebug' ) {
475 } elseif ( $channel ===
'wfLogDBError' ) {
481 if ( is_array( $logConfig ) ) {
482 $destination = $logConfig[
'destination'];
484 $destination = strval( $logConfig );
500 public static function emit( $text, $file ) {
501 if ( str_starts_with( $file,
'udp:' ) ) {
502 $transport = UDPTransport::newFromString( $file );
503 $transport->emit( $text );
506 $exists = @file_exists( $file );
508 $size = $exists ? @filesize( $file ) :
false;
510 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
513 @file_put_contents( $file, $text, FILE_APPEND );
525 private function maybeLogToStderr(
string $text ): void {
526 if ( getenv(
'MW_LOG_STDERR' ) ) {
527 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'))
A generic class to send a message over UDP.
$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.