diff options
author | dizzy <diosmosis@users.noreply.github.com> | 2021-06-28 22:53:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-28 22:53:13 +0300 |
commit | c3337d24762ec3adc1977a4820b47b1d12ceb0f0 (patch) | |
tree | 0d8f419dc1a042b420231260286e27c9056c0745 /plugins/Monolog | |
parent | 5a68eedc22515b6d3baa6ae975964c22aa7fe626 (diff) |
Add some context to this error since logs will not have the host or instance or other information. (#17472)
* Add some context to this error since logs will not have the host or instance or other information.
* Move context addition to ExceptionToTextProcessor.
* fix some tests
Diffstat (limited to 'plugins/Monolog')
3 files changed, 39 insertions, 5 deletions
diff --git a/plugins/Monolog/Processor/ExceptionToTextProcessor.php b/plugins/Monolog/Processor/ExceptionToTextProcessor.php index b4345f650a..422349da85 100644 --- a/plugins/Monolog/Processor/ExceptionToTextProcessor.php +++ b/plugins/Monolog/Processor/ExceptionToTextProcessor.php @@ -8,12 +8,14 @@ namespace Piwik\Plugins\Monolog\Processor; +use Piwik\Common; use Piwik\Db; use Piwik\ErrorHandler; use Piwik\Exception\InvalidRequestParameterException; use Piwik\Log; use Piwik\Piwik; use Piwik\SettingsPiwik; +use Piwik\Url; /** * Process a log record containing an exception to generate a textual message. @@ -55,6 +57,8 @@ class ExceptionToTextProcessor $record['message'] = str_replace('{exception}', $exceptionStr, $record['message']); } + $record['message'] .= ' [' . $this->getErrorContext() . ']'; + return $record; } @@ -144,4 +148,16 @@ class ExceptionToTextProcessor return str_replace(array_keys($valuesToReplace), array_values($valuesToReplace), $trace); } + + private function getErrorContext() + { + try { + $context = 'Query: ' . Url::getCurrentQueryString(); + $context .= ', CLI mode: ' . (int)Common::isPhpCliMode(); + return $context; + } catch (\Exception $ex) { + $context = "cannot get url or cli mode: " . $ex->getMessage(); + return $context; + } + } } diff --git a/plugins/Monolog/tests/Integration/LogTest.php b/plugins/Monolog/tests/Integration/LogTest.php index e0e2c6e53b..6353a0ed06 100644 --- a/plugins/Monolog/tests/Integration/LogTest.php +++ b/plugins/Monolog/tests/Integration/LogTest.php @@ -28,11 +28,14 @@ class LogTest extends IntegrationTestCase const STRING_MESSAGE_FORMAT = '[%tag%] %message%'; const STRING_MESSAGE_FORMAT_SPRINTF = "[%s] [%s] %s"; - public static $expectedExceptionOutput = '[Monolog] [%s] LogTest.php(112): dummy error message - dummy backtrace'; + public static $expectedExceptionOutput = '[Monolog] [%s] LogTest.php(130): dummy error message + dummy backtrace [Query: , CLI mode: 1]'; public static $expectedErrorOutput = '[Monolog] [%s] dummyerrorfile.php(145): dummy error message - dummy backtrace'; + dummy backtrace [Query: , CLI mode: 1]'; + + public static $expectedErrorOutputWithQuery = '[Monolog] [%s] dummyerrorfile.php(145): dummy error message + dummy backtrace [Query: ?a=b&d=f, CLI mode: 1]'; public function setUp(): void { @@ -105,6 +108,21 @@ class LogTest extends IntegrationTestCase /** * @dataProvider getBackendsToTest */ + public function testLoggingContextWorks($backend) + { + $this->recreateLogSingleton($backend); + + $_SERVER['QUERY_STRING'] = 'a=b&d=f'; + + $error = new \ErrorException("dummy error string", 0, 102, "dummyerrorfile.php", 145); + Log::error($error); + + $this->checkBackend($backend, sprintf(self::$expectedErrorOutputWithQuery, getmypid()), $formatMessage = false, $tag = 'Monolog'); + } + + /** + * @dataProvider getBackendsToTest + */ public function testLoggingWorksWhenMessageIsException($backend) { $this->recreateLogSingleton($backend); diff --git a/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php b/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php index b815046c52..e06049f8ba 100644 --- a/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php +++ b/plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php @@ -72,7 +72,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase $result = $processor($record); $expected = array( - 'message' => __FILE__ . "(65): [message and stack trace]", + 'message' => __FILE__ . "(65): [message and stack trace] [Query: , CLI mode: 1]", 'context' => array( 'exception' => $exception, ), @@ -99,7 +99,7 @@ class ExceptionToTextProcessorTest extends \PHPUnit\Framework\TestCase $result = $processor($record); $expected = array( - 'message' => "file.php(123): [message and stack trace]", + 'message' => "file.php(123): [message and stack trace] [Query: , CLI mode: 1]", 'context' => array( 'exception' => $exception, ), |