27use Psr\Log\AbstractLogger;
33use 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,
90 'DBConnection' =>
true
99 private $minimumLevel;
115 $this->isDB = isset( self::$dbChannels[
$channel] );
119 $this->minimumLevel = self::LEVEL_WARNING;
122 $this->minimumLevel = self::LEVEL_DEBUG;
126 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
127 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
129 $this->minimumLevel = self::LEVEL_DEBUG;
133 $this->minimumLevel = self::LEVEL_INFINITY;
136 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
138 $this->minimumLevel = self::LEVEL_ERROR;
150 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
151 throw new RuntimeException(
'Not allowed outside tests' );
154 $original = $this->minimumLevel;
155 $this->minimumLevel = $level ?? self::LEVEL_INFINITY;
166 public function log( $level, $message, array $context = [] ) {
167 if ( is_string( $level ) ) {
168 $level = self::$levelMapping[$level];
170 if ( $level < $this->minimumLevel ) {
174 if ( $this->channel ===
'DBQuery'
175 && $level === self::LEVEL_DEBUG
176 && isset( $context[
'sql'] )
183 $context[
'db_server']
193 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
195 $effectiveChannel =
'wfLogDBError';
200 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
201 $text =
self::format( $effectiveChannel, $message, $context );
205 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
224 if ( is_string( $level ) ) {
225 $level = self::$levelMapping[$level];
228 if (
$channel ===
'wfLogDBError' ) {
233 } elseif (
$channel ===
'wfDebug' ) {
243 if ( is_array( $logConfig ) ) {
245 if ( isset( $logConfig[
'sample'] ) ) {
247 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
250 if ( isset( $logConfig[
'level'] ) ) {
251 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
255 $shouldEmit = $logConfig !==
false;
258 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
291 } elseif (
$channel ===
'wfLogDBError' ) {
296 $channel,
"[{$channel}] {$message}", $context );
305 $e = $context[
'exception'];
308 if ( $e instanceof Throwable ) {
311 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
313 $backtrace = $e[
'trace'];
334 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
335 if ( isset( $context[
'seconds_elapsed'] ) ) {
338 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
340 if ( isset( $context[
'prefix'] ) ) {
341 $text =
"{$context['prefix']}{$text}";
356 static $cachedTimezone =
null;
358 if ( !$cachedTimezone ) {
362 $d = date_create(
'now', $cachedTimezone );
363 $date = $d->format(
'D M j G:i:s T Y' );
368 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
384 $text =
"{$time} {$host} {$wiki}: {$message}\n";
396 if ( strpos( $message,
'{' ) !==
false ) {
398 foreach ( $context as $key => $val ) {
401 $message = strtr( $message, $replace );
414 if ( $item ===
null ) {
418 if ( is_bool( $item ) ) {
419 return $item ?
'true' :
'false';
422 if ( is_float( $item ) ) {
423 if ( is_infinite( $item ) ) {
424 return ( $item > 0 ?
'' :
'-' ) .
'INF';
426 if ( is_nan( $item ) ) {
429 return (
string)$item;
432 if ( is_scalar( $item ) ) {
433 return (
string)$item;
436 if ( is_array( $item ) ) {
437 return '[Array(' . count( $item ) .
')]';
440 if ( $item instanceof \DateTime ) {
441 return $item->format(
'c' );
444 if ( $item instanceof Throwable ) {
445 $which = $item instanceof Error ?
'Error' :
'Exception';
446 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
447 $item->getFile() .
':' . $item->getLine() .
') ' .
448 $item->getMessage() .
']';
451 if ( is_object( $item ) ) {
452 if ( method_exists( $item,
'__toString' ) ) {
453 return (
string)$item;
456 return '[Object ' . get_class( $item ) .
']';
460 if ( is_resource( $item ) ) {
461 return '[Resource ' . get_resource_type( $item ) .
']';
464 return '[Unknown ' . gettype( $item ) .
']';
484 if ( isset( $context[
'destination'] ) ) {
486 $destination = $context[
'destination'];
488 } elseif (
$channel ===
'wfDebug' ) {
491 } elseif (
$channel ===
'wfLogDBError' ) {
497 if ( is_array( $logConfig ) ) {
498 $destination = $logConfig[
'destination'];
500 $destination = strval( $logConfig );
517 if ( substr(
$file, 0, 4 ) ==
'udp:' ) {
519 $transport->emit( $text );
521 AtEase::suppressWarnings();
522 $exists = file_exists(
$file );
523 $size = $exists ? filesize(
$file ) :
false;
525 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
527 file_put_contents(
$file, $text, FILE_APPEND );
529 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)
Helper tools for dealing with other locally-hosted wikis.
static getCurrentWikiId()
$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.