diff options
Diffstat (limited to 'includes/debug/logger/LegacyLogger.php')
-rw-r--r-- | includes/debug/logger/LegacyLogger.php | 101 |
1 files changed, 91 insertions, 10 deletions
diff --git a/includes/debug/logger/LegacyLogger.php b/includes/debug/logger/LegacyLogger.php index edaef4a7..0f4c6484 100644 --- a/includes/debug/logger/LegacyLogger.php +++ b/includes/debug/logger/LegacyLogger.php @@ -21,7 +21,9 @@ namespace MediaWiki\Logger; use DateTimeZone; +use Exception; use MWDebug; +use MWExceptionHandler; use Psr\Log\AbstractLogger; use Psr\Log\LogLevel; use UDPTransport; @@ -39,7 +41,7 @@ use UDPTransport; * See documentation in DefaultSettings.php for detailed explanations of each * variable. * - * @see \MediaWiki\Logger\LoggerFactory + * @see \\MediaWiki\\Logger\\LoggerFactory * @since 1.25 * @author Bryan Davis <bd808@wikimedia.org> * @copyright © 2014 Bryan Davis and Wikimedia Foundation. @@ -52,10 +54,10 @@ class LegacyLogger extends AbstractLogger { protected $channel; /** - * Convert Psr\Log\LogLevel constants into int for sane comparisons + * Convert Psr\\Log\\LogLevel constants into int for sane comparisons * These are the same values that Monlog uses * - * @var array + * @var array $levelMapping */ protected static $levelMapping = array( LogLevel::DEBUG => 100, @@ -99,7 +101,7 @@ class LegacyLogger extends AbstractLogger { * * @param string $channel * @param string $message - * @param string|int $level Psr\Log\LogEvent constant or Monlog level int + * @param string|int $level Psr\\Log\\LogEvent constant or Monlog level int * @param array $context * @return bool True if message should be sent to disk/network, false * otherwise @@ -167,7 +169,7 @@ class LegacyLogger extends AbstractLogger { * @return string */ public static function format( $channel, $message, $context ) { - global $wgDebugLogGroups; + global $wgDebugLogGroups, $wgLogExceptionBacktrace; if ( $channel === 'wfDebug' ) { $text = self::formatAsWfDebug( $channel, $message, $context ); @@ -181,7 +183,7 @@ class LegacyLogger extends AbstractLogger { } elseif ( $channel === 'profileoutput' ) { // Legacy wfLogProfilingData formatitng $forward = ''; - if ( isset( $context['forwarded_for'] )) { + if ( isset( $context['forwarded_for'] ) ) { $forward = " forwarded for {$context['forwarded_for']}"; } if ( isset( $context['client_ip'] ) ) { @@ -215,6 +217,25 @@ class LegacyLogger extends AbstractLogger { $text = self::formatAsWfDebugLog( $channel, $message, $context ); } + // Append stacktrace of exception if available + if ( $wgLogExceptionBacktrace && isset( $context['exception'] ) ) { + $e = $context['exception']; + $backtrace = false; + + if ( $e instanceof Exception ) { + $backtrace = MWExceptionHandler::getRedactedTrace( $e ); + + } elseif ( is_array( $e ) && isset( $e['trace'] ) ) { + // Exception has already been unpacked as structured data + $backtrace = $e['trace']; + } + + if ( $backtrace ) { + $text .= MWExceptionHandler::prettyPrintTrace( $backtrace ) . + "\n"; + } + } + return self::interpolate( $text, $context ); } @@ -253,7 +274,7 @@ class LegacyLogger extends AbstractLogger { global $wgDBerrorLogTZ; static $cachedTimezone = null; - if ( $wgDBerrorLogTZ && !$cachedTimezone ) { + if ( !$cachedTimezone ) { $cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ ); } @@ -301,7 +322,7 @@ class LegacyLogger extends AbstractLogger { if ( strpos( $message, '{' ) !== false ) { $replace = array(); foreach ( $context as $key => $val ) { - $replace['{' . $key . '}'] = $val; + $replace['{' . $key . '}'] = self::flatten( $val ); } $message = strtr( $message, $replace ); } @@ -310,6 +331,66 @@ class LegacyLogger extends AbstractLogger { /** + * Convert a logging context element to a string suitable for + * interpolation. + * + * @param mixed $item + * @return string + */ + protected static function flatten( $item ) { + if ( null === $item ) { + return '[Null]'; + } + + if ( is_bool( $item ) ) { + return $item ? 'true' : 'false'; + } + + if ( is_float( $item ) ) { + if ( is_infinite( $item ) ) { + return ( $item > 0 ? '' : '-' ) . 'INF'; + } + if ( is_nan( $item ) ) { + return 'NaN'; + } + return $item; + } + + if ( is_scalar( $item ) ) { + return (string) $item; + } + + if ( is_array( $item ) ) { + return '[Array(' . count( $item ) . ')]'; + } + + if ( $item instanceof \DateTime ) { + return $item->format( 'c' ); + } + + if ( $item instanceof Exception ) { + return '[Exception ' . get_class( $item ) . '( ' . + $item->getFile() . ':' . $item->getLine() . ') ' . + $item->getMessage() . ']'; + } + + if ( is_object( $item ) ) { + if ( method_exists( $item, '__toString' ) ) { + return (string) $item; + } + + return '[Object ' . get_class( $item ) . ']'; + } + + if ( is_resource( $item ) ) { + return '[Resource ' . get_resource_type( $item ) . ']'; + } + + return '[Unknown ' . gettype( $item ) . ']'; + } + + + /** * Select the appropriate log output destination for the given log event. * * If the event context contains 'destination' @@ -365,7 +446,7 @@ class LegacyLogger extends AbstractLogger { $transport = UDPTransport::newFromString( $file ); $transport->emit( $text ); } else { - wfSuppressWarnings(); + \MediaWiki\suppressWarnings(); $exists = file_exists( $file ); $size = $exists ? filesize( $file ) : false; if ( !$exists || @@ -373,7 +454,7 @@ class LegacyLogger extends AbstractLogger { ) { file_put_contents( $file, $text, FILE_APPEND ); } - wfRestoreWarnings(); + \MediaWiki\restoreWarnings(); } } |