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'] )
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 );
198 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
217 if ( is_string( $level ) ) {
218 $level = self::$levelMapping[$level];
221 if (
$channel ===
'wfLogDBError' ) {
226 } elseif (
$channel ===
'wfDebug' ) {
236 if ( is_array( $logConfig ) ) {
238 if ( isset( $logConfig[
'sample'] ) ) {
240 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
243 if ( isset( $logConfig[
'level'] ) ) {
244 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
248 $shouldEmit = $logConfig !==
false;
251 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
284 } elseif (
$channel ===
'wfLogDBError' ) {
289 $channel,
"[{$channel}] {$message}", $context );
298 $e = $context[
'exception'];
301 if ( $e instanceof Throwable ) {
304 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
306 $backtrace = $e[
'trace'];
327 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
328 if ( isset( $context[
'seconds_elapsed'] ) ) {
331 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
333 if ( isset( $context[
'prefix'] ) ) {
334 $text =
"{$context['prefix']}{$text}";
349 static $cachedTimezone =
null;
351 if ( !$cachedTimezone ) {
355 $d = date_create(
'now', $cachedTimezone );
356 $date = $d->format(
'D M j G:i:s T Y' );
359 $wiki = WikiMap::getCurrentWikiId();
361 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
375 $wiki = WikiMap::getCurrentWikiId();
377 $text =
"{$time} {$host} {$wiki}: {$message}\n";
389 if ( str_contains( $message,
'{' ) ) {
391 foreach ( $context as $key => $val ) {
394 $message = strtr( $message, $replace );
407 if ( $item ===
null ) {
411 if ( is_bool( $item ) ) {
412 return $item ?
'true' :
'false';
415 if ( is_float( $item ) ) {
416 if ( is_infinite( $item ) ) {
417 return ( $item > 0 ?
'' :
'-' ) .
'INF';
419 if ( is_nan( $item ) ) {
422 return (
string)$item;
425 if ( is_scalar( $item ) ) {
426 return (
string)$item;
429 if ( is_array( $item ) ) {
430 return '[Array(' . count( $item ) .
')]';
433 if ( $item instanceof \DateTime ) {
434 return $item->format(
'c' );
437 if ( $item instanceof Throwable ) {
438 $which = $item instanceof Error ?
'Error' :
'Exception';
439 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
440 $item->getFile() .
':' . $item->getLine() .
') ' .
441 $item->getMessage() .
']';
444 if ( is_object( $item ) ) {
445 if ( method_exists( $item,
'__toString' ) ) {
446 return (
string)$item;
449 return '[Object ' . get_class( $item ) .
']';
453 if ( is_resource( $item ) ) {
454 return '[Resource ' . get_resource_type( $item ) .
']';
457 return '[Unknown ' . gettype( $item ) .
']';
477 if ( isset( $context[
'destination'] ) ) {
479 $destination = $context[
'destination'];
481 } elseif (
$channel ===
'wfDebug' ) {
484 } elseif (
$channel ===
'wfLogDBError' ) {
490 if ( is_array( $logConfig ) ) {
491 $destination = $logConfig[
'destination'];
493 $destination = strval( $logConfig );
510 if ( str_starts_with(
$file,
'udp:' ) ) {
512 $transport->emit( $text );
514 AtEase::suppressWarnings();
515 $exists = file_exists(
$file );
516 $size = $exists ? filesize(
$file ) :
false;
518 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
520 file_put_contents(
$file, $text, FILE_APPEND );
522 AtEase::restoreWarnings();
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.
static query( $sql, $function, $runTime, $dbhost)
Begins profiling on a database query.
static debugMsg( $str, $context=[])
This is a method to pass messages from wfDebug to the pretty debugger.
Handler class for MWExceptions.
static getRedactedTrace(Throwable $e)
Return a copy of a throwable's backtrace as an array.
static prettyPrintTrace(array $trace, $pad='')
Generate a string representation of a stacktrace.
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.
$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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.