29 use Psr\Log\AbstractLogger;
33 use Wikimedia\AtEase\AtEase;
58 private const LEVEL_DEBUG = 100;
59 private const LEVEL_INFO = 200;
60 private const LEVEL_NOTICE = 250;
61 private const LEVEL_WARNING = 300;
62 private const LEVEL_ERROR = 400;
63 private const LEVEL_CRITICAL = 500;
64 private const LEVEL_ALERT = 550;
65 private const LEVEL_EMERGENCY = 600;
66 private const LEVEL_INFINITY = 999;
75 LogLevel::DEBUG => self::LEVEL_DEBUG,
76 LogLevel::INFO => self::LEVEL_INFO,
77 LogLevel::NOTICE => self::LEVEL_NOTICE,
78 LogLevel::WARNING => self::LEVEL_WARNING,
79 LogLevel::ERROR => self::LEVEL_ERROR,
80 LogLevel::CRITICAL => self::LEVEL_CRITICAL,
81 LogLevel::ALERT => self::LEVEL_ALERT,
82 LogLevel::EMERGENCY => self::LEVEL_EMERGENCY,
91 private $minimumLevel;
107 $this->isDB = (
$channel ===
'rdbms' );
111 $this->minimumLevel = self::LEVEL_WARNING;
114 $this->minimumLevel = self::LEVEL_DEBUG;
118 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
119 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
121 $this->minimumLevel = self::LEVEL_DEBUG;
125 $this->minimumLevel = self::LEVEL_INFINITY;
128 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
130 $this->minimumLevel = self::LEVEL_ERROR;
142 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
143 throw new LogicException(
'Not allowed outside tests' );
146 $original = $this->minimumLevel;
147 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
158 public function log( $level, $message, array $context = [] ) {
159 if ( is_string( $level ) ) {
160 $level = self::$levelMapping[$level];
162 if ( $level < $this->minimumLevel ) {
167 && $level === self::LEVEL_DEBUG
168 && isset( $context[
'sql'] )
175 $context[
'db_server']
185 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
187 $effectiveChannel =
'wfLogDBError';
192 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
193 $text =
self::format( $effectiveChannel, $message, $context );
197 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
216 if ( is_string( $level ) ) {
217 $level = self::$levelMapping[$level];
220 if (
$channel ===
'wfLogDBError' ) {
225 } elseif (
$channel ===
'wfDebug' ) {
235 if ( is_array( $logConfig ) ) {
237 if ( isset( $logConfig[
'sample'] ) ) {
239 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
242 if ( isset( $logConfig[
'level'] ) ) {
243 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
247 $shouldEmit = $logConfig !==
false;
250 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
283 } elseif (
$channel ===
'wfLogDBError' ) {
288 $channel,
"[{$channel}] {$message}", $context );
297 $e = $context[
'exception'];
300 if ( $e instanceof Throwable ) {
303 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
305 $backtrace = $e[
'trace'];
326 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
327 if ( isset( $context[
'seconds_elapsed'] ) ) {
330 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
332 if ( isset( $context[
'prefix'] ) ) {
333 $text =
"{$context['prefix']}{$text}";
348 static $cachedTimezone =
null;
350 if ( !$cachedTimezone ) {
354 $d = date_create(
'now', $cachedTimezone );
355 $date = $d->format(
'D M j G:i:s T Y' );
360 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
376 $text =
"{$time} {$host} {$wiki}: {$message}\n";
388 if ( str_contains( $message,
'{' ) ) {
390 foreach ( $context as $key => $val ) {
393 $message = strtr( $message, $replace );
406 if ( $item ===
null ) {
410 if ( is_bool( $item ) ) {
411 return $item ?
'true' :
'false';
414 if ( is_float( $item ) ) {
415 if ( is_infinite( $item ) ) {
416 return ( $item > 0 ?
'' :
'-' ) .
'INF';
418 if ( is_nan( $item ) ) {
421 return (
string)$item;
424 if ( is_scalar( $item ) ) {
425 return (
string)$item;
428 if ( is_array( $item ) ) {
429 return '[Array(' . count( $item ) .
')]';
432 if ( $item instanceof \DateTime ) {
433 return $item->format(
'c' );
436 if ( $item instanceof Throwable ) {
437 $which = $item instanceof Error ?
'Error' :
'Exception';
438 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
439 $item->getFile() .
':' . $item->getLine() .
') ' .
440 $item->getMessage() .
']';
443 if ( is_object( $item ) ) {
444 if ( method_exists( $item,
'__toString' ) ) {
445 return (
string)$item;
448 return '[Object ' . get_class( $item ) .
']';
452 if ( is_resource( $item ) ) {
453 return '[Resource ' . get_resource_type( $item ) .
']';
456 return '[Unknown ' . gettype( $item ) .
']';
476 if ( isset( $context[
'destination'] ) ) {
478 $destination = $context[
'destination'];
480 } elseif (
$channel ===
'wfDebug' ) {
483 } elseif (
$channel ===
'wfLogDBError' ) {
489 if ( is_array( $logConfig ) ) {
490 $destination = $logConfig[
'destination'];
492 $destination = strval( $logConfig );
509 if ( str_starts_with(
$file,
'udp:' ) ) {
511 $transport->emit( $text );
513 AtEase::suppressWarnings();
514 $exists = file_exists(
$file );
515 $size = $exists ? filesize(
$file ) :
false;
517 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
519 file_put_contents(
$file, $text, FILE_APPEND );
521 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.
New debugger system that outputs a toolbar on page view.
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.