From: Bryan Davis Date: Thu, 30 Jul 2015 19:02:35 +0000 (-0600) Subject: Monolog: Add Formatter that uses MWExceptionHandler::getRedactedTraceAsString X-Git-Tag: 1.31.0-rc.0~10563^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=27dcc9f051821d2c25b4411d2838a91d9f6740df;p=lhc%2Fweb%2Fwiklou.git Monolog: Add Formatter that uses MWExceptionHandler::getRedactedTraceAsString Add a Monolog Formatter class that uses MWExceptionHandler::getRedactedTraceAsString when outputting stack traces. Bug: T107440 Change-Id: Ic580c137e27aac95435f7b073a18cf61820b172f --- diff --git a/autoload.php b/autoload.php index 4109740772..28884c3dd4 100644 --- a/autoload.php +++ b/autoload.php @@ -754,6 +754,7 @@ $wgAutoloadLocalClasses = array( 'MediaWiki\\Logger\\MonologSpi' => __DIR__ . '/includes/debug/logger/MonologSpi.php', 'MediaWiki\\Logger\\Monolog\\LegacyFormatter' => __DIR__ . '/includes/debug/logger/monolog/LegacyFormatter.php', 'MediaWiki\\Logger\\Monolog\\LegacyHandler' => __DIR__ . '/includes/debug/logger/monolog/LegacyHandler.php', + 'MediaWiki\\Logger\\Monolog\\LineFormatter' => __DIR__ . '/includes/debug/logger/monolog/LineFormatter.php', 'MediaWiki\\Logger\\Monolog\\SyslogHandler' => __DIR__ . '/includes/debug/logger/monolog/SyslogHandler.php', 'MediaWiki\\Logger\\Monolog\\WikiProcessor' => __DIR__ . '/includes/debug/logger/monolog/WikiProcessor.php', 'MediaWiki\\Logger\\NullSpi' => __DIR__ . '/includes/debug/logger/NullSpi.php', diff --git a/includes/debug/logger/LegacyLogger.php b/includes/debug/logger/LegacyLogger.php index 831ad1b425..b6439b8222 100644 --- a/includes/debug/logger/LegacyLogger.php +++ b/includes/debug/logger/LegacyLogger.php @@ -222,7 +222,7 @@ class LegacyLogger extends AbstractLogger { $context['exception'] instanceof Exception ) { $text .= MWExceptionHandler::getRedactedTraceAsString( - $context['exception']->getTraceAsString() + $context['exception'] ) . "\n"; } diff --git a/includes/debug/logger/monolog/LineFormatter.php b/includes/debug/logger/monolog/LineFormatter.php new file mode 100644 index 0000000000..e593d63bc4 --- /dev/null +++ b/includes/debug/logger/monolog/LineFormatter.php @@ -0,0 +1,87 @@ + + * @copyright © 2015 Bryan Davis and Wikimedia Foundation. + */ +class LineFormatter extends MonologLineFormatter { + + /** + * @param string $format The format of the message + * @param string $dateFormat The format of the timestamp: one supported by DateTime::format + * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries + * @param bool $ignoreEmptyContextAndExtra + * @param bool $includeStacktraces + */ + public function __construct( + $format = null, $dateFormat = null, $allowInlineLineBreaks = false, + $ignoreEmptyContextAndExtra = false, $includeStacktraces = false + ) { + parent::__construct( + $format, $dateFormat, $allowInlineLineBreaks, + $ignoreEmptyContextAndExtra + ); + $this->includeStacktraces( $includeStacktraces ); + } + + + /** + * Convert an Exception to a string. + * + * @param Exception $e + * @return string + */ + protected function normalizeException( Exception $e ) { + $str = '[Exception ' . get_class( $e ) . '] (' . + $e->getFile() . ':' . $e->getLine() . ') ' . + $e->getMessage(); + + $prev = $e->getPrevious(); + while ( $prev ) { + $str .= ', [Exception ' . get_class( $prev ) . '] (' . + $prev->getFile() . ':' . $prev->getLine() . ') ' . + $prev->getMessage(); + $prev = $prev->getPrevious(); + } + + if ( $this->includeStacktraces ) { + $str .= "\n[stacktrace]\n" . + MWExceptionHandler::getRedactedTraceAsString( $e ) . + "\n"; + } + + return $str; + } +} diff --git a/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php b/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php new file mode 100644 index 0000000000..05c32a0463 --- /dev/null +++ b/tests/phpunit/includes/debug/logger/monolog/LineFormatterTest.php @@ -0,0 +1,68 @@ +includeStacktraces( false ); + $fixture = TestingAccessWrapper::newFromObject( $fixture ); + $boom = new InvalidArgumentException( 'boom', 0, + new LengthException( 'too long', 0, + new LogicException( 'Spock wuz here' ) + ) + ); + $out = $fixture->normalizeException( $boom ); + $this->assertContains( '[Exception InvalidArgumentException]', $out ); + $this->assertContains( ', [Exception LengthException]', $out ); + $this->assertContains( ', [Exception LogicException]', $out ); + $this->assertNotContains( '[stacktrace]', $out ); + } + + /** + * @covers LineFormatter::normalizeException + */ + public function testNormalizeExceptionTrace() { + $fixture = new LineFormatter(); + $fixture->includeStacktraces( true ); + $fixture = TestingAccessWrapper::newFromObject( $fixture ); + $boom = new InvalidArgumentException( 'boom', 0, + new LengthException( 'too long', 0, + new LogicException( 'Spock wuz here' ) + ) + ); + $out = $fixture->normalizeException( $boom ); + $this->assertContains( '[Exception InvalidArgumentException', $out ); + $this->assertContains( ', [Exception LengthException]', $out ); + $this->assertContains( ', [Exception LogicException]', $out ); + $this->assertContains( '[stacktrace]', $out ); + } +}