27use Psr\Log\AbstractLogger;
90 'DBConnection' =>
true
115 $this->isDB = isset( self::$dbChannels[
$channel] );
124 if ( is_array( $logConfig ) && isset( $logConfig[
'level'] ) ) {
125 $this->minimumLevel = self::$levelMapping[$logConfig[
'level']];
133 if ( $this->isDB &&
$wgDBerrorLog && $this->minimumLevel > self::LEVEL_ERROR ) {
147 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
148 throw new RuntimeException(
'Not allowed outside tests' );
163 public function log( $level, $message, array $context = [] ) {
164 if ( is_string( $level ) ) {
165 $level = self::$levelMapping[$level];
167 if ( $level < $this->minimumLevel ) {
171 if ( $this->channel ===
'DBQuery'
172 && $level === self::LEVEL_DEBUG
173 && isset( $context[
'sql'] )
180 $context[
'db_server']
190 if ( $this->isDB && $level >= self::LEVEL_ERROR &&
$wgDBerrorLog ) {
192 $effectiveChannel =
'wfLogDBError';
197 if ( self::shouldEmit( $effectiveChannel, $message, $level, $context ) ) {
198 $text =
self::format( $effectiveChannel, $message, $context );
202 if ( !isset( $context[
'private'] ) || !$context[
'private'] ) {
221 if ( is_string( $level ) ) {
222 $level = self::$levelMapping[$level];
225 if (
$channel ===
'wfLogDBError' ) {
230 } elseif (
$channel ===
'wfDebug' ) {
240 if ( is_array( $logConfig ) ) {
242 if ( isset( $logConfig[
'sample'] ) ) {
244 $shouldEmit = mt_rand( 1, $logConfig[
'sample'] ) === 1;
247 if ( isset( $logConfig[
'level'] ) ) {
248 $shouldEmit = $level >= self::$levelMapping[$logConfig[
'level']];
252 $shouldEmit = $logConfig !==
false;
255 } elseif ( isset( $context[
'private'] ) && $context[
'private'] ) {
288 } elseif (
$channel ===
'wfLogDBError' ) {
293 $channel,
"[{$channel}] {$message}", $context );
302 $e = $context[
'exception'];
305 if ( $e instanceof Throwable ) {
308 } elseif ( is_array( $e ) && isset( $e[
'trace'] ) ) {
310 $backtrace = $e[
'trace'];
331 $text = preg_replace(
'![\x00-\x08\x0b\x0c\x0e-\x1f]!',
' ', $message );
332 if ( isset( $context[
'seconds_elapsed'] ) ) {
335 $text =
"{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
337 if ( isset( $context[
'prefix'] ) ) {
338 $text =
"{$context['prefix']}{$text}";
353 static $cachedTimezone =
null;
355 if ( !$cachedTimezone ) {
359 $d = date_create(
'now', $cachedTimezone );
360 $date = $d->format(
'D M j G:i:s T Y' );
365 $text =
"{$date}\t{$host}\t{$wiki}\t{$message}\n";
381 $text =
"{$time} {$host} {$wiki}: {$message}\n";
393 if ( strpos( $message,
'{' ) !==
false ) {
395 foreach ( $context as $key => $val ) {
398 $message = strtr( $message, $replace );
411 if ( $item ===
null ) {
415 if ( is_bool( $item ) ) {
416 return $item ?
'true' :
'false';
419 if ( is_float( $item ) ) {
420 if ( is_infinite( $item ) ) {
421 return ( $item > 0 ?
'' :
'-' ) .
'INF';
423 if ( is_nan( $item ) ) {
426 return (
string)$item;
429 if ( is_scalar( $item ) ) {
430 return (
string)$item;
433 if ( is_array( $item ) ) {
434 return '[Array(' . count( $item ) .
')]';
437 if ( $item instanceof \DateTime ) {
438 return $item->format(
'c' );
441 if ( $item instanceof Throwable ) {
442 $which = $item instanceof Error ?
'Error' :
'Exception';
443 return '[' . $which .
' ' . get_class( $item ) .
'( ' .
444 $item->getFile() .
':' . $item->getLine() .
') ' .
445 $item->getMessage() .
']';
448 if ( is_object( $item ) ) {
449 if ( method_exists( $item,
'__toString' ) ) {
450 return (
string)$item;
453 return '[Object ' . get_class( $item ) .
']';
457 if ( is_resource( $item ) ) {
458 return '[Resource ' . get_resource_type( $item ) .
']';
461 return '[Unknown ' . gettype( $item ) .
']';
481 if ( isset( $context[
'destination'] ) ) {
483 $destination = $context[
'destination'];
485 } elseif (
$channel ===
'wfDebug' ) {
488 } elseif (
$channel ===
'wfLogDBError' ) {
494 if ( is_array( $logConfig ) ) {
495 $destination = $logConfig[
'destination'];
497 $destination = strval( $logConfig );
514 if ( substr(
$file, 0, 4 ) ==
'udp:' ) {
516 $transport->emit( $text );
518 \Wikimedia\suppressWarnings();
519 $exists = file_exists(
$file );
520 $size = $exists ? filesize(
$file ) :
false;
522 ( $size !==
false && $size + strlen( $text ) < 0x7fffffff )
524 file_put_contents(
$file, $text, FILE_APPEND );
526 \Wikimedia\restoreWarnings();
$wgLogExceptionBacktrace
If true, send the exception backtrace to the error log.
$wgDBerrorLogTZ
Timezone to use in the error log.
$wgDBerrorLog
File to log database errors to.
$wgDebugToolbar
Display the new debugging toolbar.
$wgDebugLogGroups
Map of string log group names to log destinations.
$wgDebugLogFile
Filename for debug logging.
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()
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.