2014-03-21 05:01:24 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PSR-3 logger that mimics the historic implementation of MediaWiki's
|
|
|
|
|
* wfErrorLog logging implementation.
|
|
|
|
|
*
|
|
|
|
|
* This logger is configured by the following global configuration variables:
|
|
|
|
|
* - `$wgDebugLogFile`
|
|
|
|
|
* - `$wgDebugLogGroups`
|
|
|
|
|
* - `$wgDBerrorLog`
|
|
|
|
|
* - `$wgDBerrorLogTZ`
|
|
|
|
|
*
|
|
|
|
|
* See documentation in DefaultSettings.php for detailed explanations of each
|
|
|
|
|
* variable.
|
|
|
|
|
*
|
|
|
|
|
* @see MWLogger
|
|
|
|
|
* @since 1.25
|
|
|
|
|
* @author Bryan Davis <bd808@wikimedia.org>
|
|
|
|
|
* @copyright © 2014 Bryan Davis and Wikimedia Foundation.
|
|
|
|
|
*/
|
|
|
|
|
class MWLoggerLegacyLogger extends \Psr\Log\AbstractLogger {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $channel
|
|
|
|
|
*/
|
|
|
|
|
protected $channel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $channel
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $channel ) {
|
|
|
|
|
$this->channel = $channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs with an arbitrary level.
|
|
|
|
|
*
|
|
|
|
|
* @param string|int $level
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
*/
|
|
|
|
|
public function log( $level, $message, array $context = array() ) {
|
|
|
|
|
if ( self::shouldEmit( $this->channel, $message, $context ) ) {
|
|
|
|
|
$text = self::format( $this->channel, $message, $context );
|
|
|
|
|
$destination = self::destination( $this->channel, $message, $context );
|
|
|
|
|
self::emit( $text, $destination );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the given message should be emitted or not.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return bool True if message should be sent to disk/network, false
|
|
|
|
|
* otherwise
|
|
|
|
|
*/
|
2014-11-21 06:23:49 +00:00
|
|
|
public static function shouldEmit( $channel, $message, $context ) {
|
2014-03-21 05:01:24 +00:00
|
|
|
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups;
|
|
|
|
|
|
|
|
|
|
if ( $channel === 'wfLogDBError' ) {
|
|
|
|
|
// wfLogDBError messages are emitted if a database log location is
|
|
|
|
|
// specfied.
|
2014-10-30 16:50:19 +00:00
|
|
|
$shouldEmit = (bool)$wgDBerrorLog;
|
2014-03-21 05:01:24 +00:00
|
|
|
|
|
|
|
|
} elseif ( $channel === 'wfErrorLog' ) {
|
|
|
|
|
// All messages on the wfErrorLog channel should be emitted.
|
|
|
|
|
$shouldEmit = true;
|
|
|
|
|
|
|
|
|
|
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
|
|
|
|
|
$logConfig = $wgDebugLogGroups[$channel];
|
|
|
|
|
|
|
|
|
|
if ( is_array( $logConfig ) && isset( $logConfig['sample'] ) ) {
|
|
|
|
|
// Emit randomly with a 1 in 'sample' chance for each message.
|
|
|
|
|
$shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Emit unless the config value is explictly false.
|
|
|
|
|
$shouldEmit = $logConfig !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} elseif ( isset( $context['private'] ) && $context['private'] ) {
|
2014-11-21 06:23:49 +00:00
|
|
|
// Don't emit if the message didn't match previous checks based on
|
|
|
|
|
// the channel and the event is marked as private. This check
|
|
|
|
|
// discards messages sent via wfDebugLog() with dest == 'private'
|
|
|
|
|
// and no explicit wgDebugLogGroups configuration.
|
2014-03-21 05:01:24 +00:00
|
|
|
$shouldEmit = false;
|
|
|
|
|
} else {
|
2014-12-16 00:41:45 +00:00
|
|
|
// Default return value is the same as the historic wfDebug
|
2014-03-21 05:01:24 +00:00
|
|
|
// method: emit if $wgDebugLogFile has been set.
|
|
|
|
|
$shouldEmit = $wgDebugLogFile != '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $shouldEmit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a message.
|
|
|
|
|
*
|
|
|
|
|
* Messages to the 'wfDebug', 'wfLogDBError' and 'wfErrorLog' channels
|
|
|
|
|
* receive special fomatting to mimic the historic output of the functions
|
|
|
|
|
* of the same name. All other channel values are formatted based on the
|
|
|
|
|
* historic output of the `wfDebugLog()` global function.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-11-21 00:22:37 +00:00
|
|
|
public static function format( $channel, $message, $context ) {
|
2014-03-21 05:01:24 +00:00
|
|
|
global $wgDebugLogGroups;
|
|
|
|
|
|
|
|
|
|
if ( $channel === 'wfDebug' ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$text = self::formatAsWfDebug( $channel, $message, $context );
|
2014-03-21 05:01:24 +00:00
|
|
|
|
|
|
|
|
} elseif ( $channel === 'wfLogDBError' ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$text = self::formatAsWfLogDBError( $channel, $message, $context );
|
2014-03-21 05:01:24 +00:00
|
|
|
|
|
|
|
|
} elseif ( $channel === 'wfErrorLog' ) {
|
|
|
|
|
$text = "{$message}\n";
|
|
|
|
|
|
2014-03-21 06:57:25 +00:00
|
|
|
} elseif ( $channel === 'profileoutput' ) {
|
|
|
|
|
// Legacy wfLogProfilingData formatitng
|
|
|
|
|
$forward = '';
|
|
|
|
|
if ( isset( $context['forwarded_for'] )) {
|
|
|
|
|
$forward = " forwarded for {$context['forwarded_for']}";
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $context['client_ip'] ) ) {
|
|
|
|
|
$forward .= " client IP {$context['client_ip']}";
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $context['from'] ) ) {
|
|
|
|
|
$forward .= " from {$context['from']}";
|
|
|
|
|
}
|
|
|
|
|
if ( $forward ) {
|
|
|
|
|
$forward = "\t(proxied via {$context['proxy']}{$forward})";
|
|
|
|
|
}
|
|
|
|
|
if ( $context['anon'] ) {
|
|
|
|
|
$forward .= ' anon';
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $context['url'] ) ) {
|
|
|
|
|
$context['url'] = 'n/a';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$log = sprintf( "%s\t%04.3f\t%s%s\n",
|
|
|
|
|
gmdate( 'YmdHis' ), $context['elapsed'], $context['url'], $forward );
|
|
|
|
|
|
|
|
|
|
$text = self::formatAsWfDebugLog(
|
|
|
|
|
$channel, $log . $context['output'], $context );
|
|
|
|
|
|
2014-03-21 05:01:24 +00:00
|
|
|
} elseif ( !isset( $wgDebugLogGroups[$channel] ) ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$text = self::formatAsWfDebug(
|
2014-03-21 05:01:24 +00:00
|
|
|
$channel, "[{$channel}] {$message}", $context );
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Default formatting is wfDebugLog's historic style
|
2014-03-21 06:57:25 +00:00
|
|
|
$text = self::formatAsWfDebugLog( $channel, $message, $context );
|
2014-03-21 05:01:24 +00:00
|
|
|
}
|
2014-06-23 22:25:55 +00:00
|
|
|
|
|
|
|
|
return self::interpolate( $text, $context );
|
2014-03-21 05:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a message as `wfDebug()` would have formatted it.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-03-21 06:57:25 +00:00
|
|
|
protected static function formatAsWfDebug( $channel, $message, $context ) {
|
2014-03-21 05:01:24 +00:00
|
|
|
$text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $message );
|
2014-06-23 22:25:55 +00:00
|
|
|
if ( isset( $context['seconds_elapsed'] ) ) {
|
|
|
|
|
// Prepend elapsed request time and real memory usage with two
|
|
|
|
|
// trailing spaces.
|
|
|
|
|
$text = "{$context['seconds_elapsed']} {$context['memory_used']} {$text}";
|
|
|
|
|
}
|
2014-03-21 05:01:24 +00:00
|
|
|
if ( isset( $context['prefix'] ) ) {
|
|
|
|
|
$text = "{$context['prefix']}{$text}";
|
|
|
|
|
}
|
|
|
|
|
return "{$text}\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a message as `wfLogDBError()` would have formatted it.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-03-21 06:57:25 +00:00
|
|
|
protected static function formatAsWfLogDBError( $channel, $message, $context ) {
|
2014-03-21 05:01:24 +00:00
|
|
|
global $wgDBerrorLogTZ;
|
|
|
|
|
static $cachedTimezone = null;
|
|
|
|
|
|
|
|
|
|
if ( $wgDBerrorLogTZ && !$cachedTimezone ) {
|
|
|
|
|
$cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Workaround for https://bugs.php.net/bug.php?id=52063
|
2014-10-24 01:21:50 +00:00
|
|
|
// Can be removed when min PHP > 5.3.6
|
2014-03-21 05:01:24 +00:00
|
|
|
if ( $cachedTimezone === null ) {
|
|
|
|
|
$d = date_create( 'now' );
|
|
|
|
|
} else {
|
|
|
|
|
$d = date_create( 'now', $cachedTimezone );
|
|
|
|
|
}
|
|
|
|
|
$date = $d->format( 'D M j G:i:s T Y' );
|
|
|
|
|
|
|
|
|
|
$host = wfHostname();
|
|
|
|
|
$wiki = wfWikiID();
|
|
|
|
|
|
|
|
|
|
$text = "{$date}\t{$host}\t{$wiki}\t{$message}\n";
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-21 06:57:25 +00:00
|
|
|
/**
|
|
|
|
|
* Format a message as `wfDebugLog() would have formatted it.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
*/
|
|
|
|
|
protected static function formatAsWfDebugLog( $channel, $message, $context ) {
|
|
|
|
|
$time = wfTimestamp( TS_DB );
|
|
|
|
|
$wiki = wfWikiID();
|
|
|
|
|
$host = wfHostname();
|
|
|
|
|
$text = "{$time} {$host} {$wiki}: {$message}\n";
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-23 22:25:55 +00:00
|
|
|
/**
|
|
|
|
|
* Interpolate placeholders in logging message.
|
|
|
|
|
*
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
*/
|
|
|
|
|
public static function interpolate( $message, array $context ) {
|
|
|
|
|
if ( strpos( $message, '{' ) !== false ) {
|
|
|
|
|
$replace = array();
|
|
|
|
|
foreach ( $context as $key => $val ) {
|
|
|
|
|
$replace['{' . $key . '}'] = $val;
|
|
|
|
|
}
|
|
|
|
|
$message = strtr( $message, $replace );
|
|
|
|
|
}
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-21 05:01:24 +00:00
|
|
|
/**
|
|
|
|
|
* Select the appropriate log output destination for the given log event.
|
|
|
|
|
*
|
|
|
|
|
* If the event context contains 'destination'
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param array $context
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function destination( $channel, $message, $context ) {
|
|
|
|
|
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups;
|
|
|
|
|
|
|
|
|
|
// Default destination is the debug log file as historically used by
|
|
|
|
|
// the wfDebug function.
|
|
|
|
|
$destination = $wgDebugLogFile;
|
|
|
|
|
|
|
|
|
|
if ( isset( $context['destination'] ) ) {
|
|
|
|
|
// Use destination explicitly provided in context
|
|
|
|
|
$destination = $context['destination'];
|
|
|
|
|
|
|
|
|
|
} elseif ( $channel === 'wfDebug' ) {
|
|
|
|
|
$destination = $wgDebugLogFile;
|
|
|
|
|
|
|
|
|
|
} elseif ( $channel === 'wfLogDBError' ) {
|
|
|
|
|
$destination = $wgDBerrorLog;
|
|
|
|
|
|
|
|
|
|
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
|
|
|
|
|
$logConfig = $wgDebugLogGroups[$channel];
|
|
|
|
|
|
|
|
|
|
if ( is_array( $logConfig ) ) {
|
|
|
|
|
$destination = $logConfig['destination'];
|
|
|
|
|
} else {
|
|
|
|
|
$destination = strval( $logConfig );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $destination;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log to a file without getting "file size exceeded" signals.
|
|
|
|
|
*
|
|
|
|
|
* Can also log to UDP with the syntax udp://host:port/prefix. This will send
|
|
|
|
|
* lines to the specified port, prefixed by the specified prefix and a space.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param string $file Filename
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
public static function emit( $text, $file ) {
|
|
|
|
|
if ( substr( $file, 0, 4 ) == 'udp:' ) {
|
2014-12-15 22:05:13 +00:00
|
|
|
$transport = UDPTransport::newFromString( $file );
|
|
|
|
|
$transport->emit( $text );
|
2014-03-21 05:01:24 +00:00
|
|
|
} else {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$exists = file_exists( $file );
|
|
|
|
|
$size = $exists ? filesize( $file ) : false;
|
|
|
|
|
if ( !$exists ||
|
|
|
|
|
( $size !== false && $size + strlen( $text ) < 0x7fffffff )
|
|
|
|
|
) {
|
|
|
|
|
file_put_contents( $file, $text, FILE_APPEND );
|
|
|
|
|
}
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|