diff --git a/CHANGELOG.md b/CHANGELOG.md index c7733ac3..e587d687 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ * Added support for RFC3164 (outdated BSD syslog protocol) to SyslogUdpHandler * Fixed issue in GroupHandler and WhatFailureGroupHandler where setting multiple processors would duplicate records * Fixed issue in SignalHandler restarting syscalls functionality + * Fixed normalizers handling of exception backtraces to avoid serializing arguments in some cases * Fixed ZendMonitorHandler to work with the latest Zend Server versions ### 1.24.0 (2018-11-05) diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index 3f6022b6..45fbe13f 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -178,12 +178,6 @@ class JsonFormatter extends NormalizerFormatter foreach ($trace as $frame) { if (isset($frame['file'])) { $data['trace'][] = $frame['file'].':'.$frame['line']; - } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $frame['function']; - } else { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $this->normalize($frame); } } } diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index b806ccd3..9c42a735 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -211,23 +211,6 @@ class NormalizerFormatter implements FormatterInterface foreach ($trace as $frame) { if (isset($frame['file'])) { $data['trace'][] = $frame['file'].':'.$frame['line']; - } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { - // Simplify closures handling - $data['trace'][] = $frame['function']; - } else { - if (isset($frame['args'])) { - // Make sure that objects present as arguments are not serialized nicely but rather only - // as a class name to avoid any unexpected leak of sensitive information - $frame['args'] = array_map(function ($arg) { - if (is_object($arg) && !$arg instanceof \DateTimeInterface) { - return sprintf("[object] (%s)", Utils::getClass($arg)); - } - - return $arg; - }, $frame['args']); - } - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $this->toJson($this->normalize($frame, $depth + 1), true); } } @@ -379,12 +362,12 @@ class NormalizerFormatter implements FormatterInterface return $date->format($this->dateFormat); } - + protected function addJsonEncodeOption($option) { $this->jsonEncodeOptions |= $option; } - + protected function removeJsonEncodeOption($option) { $this->jsonEncodeOptions ^= $option; diff --git a/tests/Monolog/Formatter/NormalizerFormatterTest.php b/tests/Monolog/Formatter/NormalizerFormatterTest.php index c0feb6d6..34c5d4bd 100644 --- a/tests/Monolog/Formatter/NormalizerFormatterTest.php +++ b/tests/Monolog/Formatter/NormalizerFormatterTest.php @@ -412,46 +412,7 @@ class NormalizerFormatterTest extends TestCase $result = $formatter->format($record); $this->assertSame( - '{"function":"Monolog\\\\Formatter\\\\{closure}","class":"Monolog\\\\Formatter\\\\NormalizerFormatterTest","type":"->","args":["[object] (Monolog\\\\Formatter\\\\TestFooNorm)","[resource(stream)]"]}', - $result['context']['exception']['trace'][0] - ); - } - - /** - * This test was copied from `testExceptionTraceWithArgs` in order to ensure that pretty prints works - */ - public function testPrettyPrint() - { - try { - // This will contain $resource and $wrappedResource as arguments in the trace item - $resource = fopen('php://memory', 'rw+'); - fwrite($resource, 'test_resource'); - $wrappedResource = new TestFooNorm; - $wrappedResource->foo = $resource; - // Just do something stupid with a resource/wrapped resource as argument - $arr = [$wrappedResource, $resource]; - // modifying the array inside throws a "usort(): Array was modified by the user comparison function" - usort($arr, function ($a, $b) { - throw new \ErrorException('Foo'); - }); - } catch (\Throwable $e) { - } - - $formatter = new NormalizerFormatter(); - $record = ['context' => ['exception' => $e]]; - $formatter->setJsonPrettyPrint(true); - $result = $formatter->format($record); - - $this->assertSame( - '{ - "function": "Monolog\\\\Formatter\\\\{closure}", - "class": "Monolog\\\\Formatter\\\\NormalizerFormatterTest", - "type": "->", - "args": [ - "[object] (Monolog\\\\Formatter\\\\TestFooNorm)", - "[resource(stream)]" - ] -}', + __FILE__.':'.(__LINE__-9), $result['context']['exception']['trace'][0] ); } @@ -489,7 +450,7 @@ class NormalizerFormatterTest extends TestCase $result = $formatter->format($record); $this->assertSame( - '{"function":"throwHelper","class":"Monolog\\\\Formatter\\\\NormalizerFormatterTest","type":"->","args":["[object] (Monolog\\\\Formatter\\\\TestInfoLeak)","'.$dt->format('Y-m-d\TH:i:sP').'"]}', + __FILE__ .':'.(__LINE__-9), $result['context']['exception']['trace'][0] ); } diff --git a/tests/Monolog/Formatter/ScalarFormatterTest.php b/tests/Monolog/Formatter/ScalarFormatterTest.php index 6c519628..c72227f7 100644 --- a/tests/Monolog/Formatter/ScalarFormatterTest.php +++ b/tests/Monolog/Formatter/ScalarFormatterTest.php @@ -29,8 +29,6 @@ class ScalarFormatterTest extends \PHPUnit\Framework\TestCase foreach ($trace as $frame) { if (isset($frame['file'])) { $data[] = $frame['file'].':'.$frame['line']; - } else { - $data[] = json_encode($frame); } }