From b1bb297af991a948cbb5ab7af198e3a72cb51e1d Mon Sep 17 00:00:00 2001 From: dizzy Date: Thu, 25 Mar 2021 11:38:03 -0700 Subject: allow forcing backtrace to print in exceptiontotextprocessor (#17369) * Allow forcing backtrace to print and do not print redundant message in exceptiontotextprocessor * fix test * consistent output when exception is an array * fix tests * fix another test --- plugins/Monolog/Processor/ExceptionToTextProcessor.php | 17 ++++++++++++----- plugins/Monolog/tests/Integration/LogTest.php | 4 ++-- .../Unit/Processor/ExceptionToTextProcessorTest.php | 11 ++++++----- 3 files changed, 20 insertions(+), 12 deletions(-) (limited to 'plugins/Monolog') diff --git a/plugins/Monolog/Processor/ExceptionToTextProcessor.php b/plugins/Monolog/Processor/ExceptionToTextProcessor.php index 686fd92644..b4345f650a 100644 --- a/plugins/Monolog/Processor/ExceptionToTextProcessor.php +++ b/plugins/Monolog/Processor/ExceptionToTextProcessor.php @@ -20,6 +20,13 @@ use Piwik\SettingsPiwik; */ class ExceptionToTextProcessor { + private $forcePrintBacktrace = false; + + public function __construct($forcePrintBacktrace = false) + { + $this->forcePrintBacktrace = $forcePrintBacktrace; + } + public function __invoke(array $record) { if (! $this->contextContainsException($record)) { @@ -34,10 +41,9 @@ class ExceptionToTextProcessor } $exceptionStr = sprintf( - "%s(%d): %s\n%s", + "%s(%d): %s", $exception instanceof \Exception ? $exception->getFile() : $exception['file'], $exception instanceof \Exception ? $exception->getLine() : $exception['line'], - $this->getMessage($exception), $this->getStackTrace($exception) ); @@ -79,7 +85,7 @@ class ExceptionToTextProcessor private function getStackTrace($exception) { - return Log::$debugBacktraceForTests ?: self::getMessageAndWholeBacktrace($exception); + return Log::$debugBacktraceForTests ?: self::getMessageAndWholeBacktrace($exception, $this->forcePrintBacktrace ? true : null); } /** @@ -94,12 +100,13 @@ class ExceptionToTextProcessor } if (is_array($exception)) { + $message = $exception['message'] ?? ''; if ($shouldPrintBacktrace && isset($exception['backtrace'])) { $trace = $exception['backtrace']; $trace = self::replaceSensitiveValues($trace); - return $trace; + return $message . "\n" . $trace; } else { - return ''; + return $message; } } diff --git a/plugins/Monolog/tests/Integration/LogTest.php b/plugins/Monolog/tests/Integration/LogTest.php index 9e24f4af13..e0e2c6e53b 100644 --- a/plugins/Monolog/tests/Integration/LogTest.php +++ b/plugins/Monolog/tests/Integration/LogTest.php @@ -31,7 +31,7 @@ class LogTest extends IntegrationTestCase public static $expectedExceptionOutput = '[Monolog] [%s] LogTest.php(112): dummy error message dummy backtrace'; - public static $expectedErrorOutput = '[Monolog] [%s] dummyerrorfile.php(145): Unknown error (102) - dummy error string + public static $expectedErrorOutput = '[Monolog] [%s] dummyerrorfile.php(145): dummy error message dummy backtrace'; public function setUp(): void @@ -41,7 +41,7 @@ class LogTest extends IntegrationTestCase Log::unsetInstance(); @unlink(self::getLogFileLocation()); - Log::$debugBacktraceForTests = "dummy backtrace"; + Log::$debugBacktraceForTests = "dummy error message\ndummy backtrace"; } public function tearDown(): void diff --git a/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php b/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php index 20412dc85f..b815046c52 100644 --- a/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php +++ b/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php @@ -60,7 +60,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase public function it_should_replace_message_with_formatted_exception() { $processor = new ExceptionToTextProcessor(); - Log::$debugBacktraceForTests = '[stack trace]'; + Log::$debugBacktraceForTests = '[message and stack trace]'; $exception = new \Exception('Hello world'); $record = array( @@ -72,7 +72,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase $result = $processor($record); $expected = array( - 'message' => __FILE__ . "(65): Hello world\n[stack trace]", + 'message' => __FILE__ . "(65): [message and stack trace]", 'context' => array( 'exception' => $exception, ), @@ -87,7 +87,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase public function it_should_add_severity_for_errors() { $processor = new ExceptionToTextProcessor(); - Log::$debugBacktraceForTests = '[stack trace]'; + Log::$debugBacktraceForTests = '[message and stack trace]'; $exception = new \ErrorException('Hello world', 0, 1, 'file.php', 123); $record = array( @@ -99,7 +99,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase $result = $processor($record); $expected = array( - 'message' => "file.php(123): Error - Hello world\n[stack trace]", + 'message' => "file.php(123): [message and stack trace]", 'context' => array( 'exception' => $exception, ), @@ -240,6 +240,7 @@ EOI; $wholeTrace = ExceptionToTextProcessor::getMessageAndWholeBacktrace($exArray); $expected = <<assertEquals($expected, $wholeTrace); } -- cgit v1.2.3