mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-09 14:46:46 +02:00
Add stack traces to normalized exceptions, closes #192
This commit is contained in:
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Monolog\Formatter;
|
namespace Monolog\Formatter;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
|
* Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
|
||||||
*
|
*
|
||||||
@@ -71,6 +73,10 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_object($data)) {
|
if (is_object($data)) {
|
||||||
|
if ($data instanceof Exception) {
|
||||||
|
return $this->normalizeException($data);
|
||||||
|
}
|
||||||
|
|
||||||
return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
|
return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +87,31 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
return '[unknown('.gettype($data).')]';
|
return '[unknown('.gettype($data).')]';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function normalizeException(Exception $e)
|
||||||
|
{
|
||||||
|
$data = array(
|
||||||
|
'class' => get_class($e),
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
'file' => $e->getFile().':'.$e->getLine(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$trace = $e->getTrace();
|
||||||
|
array_shift($trace);
|
||||||
|
foreach ($trace as $frame) {
|
||||||
|
if (isset($frame['file'])) {
|
||||||
|
$data['trace'][] = $frame['file'].':'.$frame['line'];
|
||||||
|
} else {
|
||||||
|
$data['trace'][] = json_encode($frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($previous = $e->getPrevious()) {
|
||||||
|
$data['previous'] = $this->normalizeException($previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
protected function toJson($data, $ignoreErrors = false)
|
protected function toJson($data, $ignoreErrors = false)
|
||||||
{
|
{
|
||||||
// suppress json_encode errors since it's twitchy with some inputs
|
// suppress json_encode errors since it's twitchy with some inputs
|
||||||
|
@@ -28,7 +28,7 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
'context' => array(
|
'context' => array(
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
'baz' => 'qux',
|
'baz' => 'qux',
|
||||||
)
|
),
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
@@ -49,6 +49,28 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
), $formatted);
|
), $formatted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFormatExceptions()
|
||||||
|
{
|
||||||
|
$formatter = new NormalizerFormatter('Y-m-d');
|
||||||
|
$e = new \LogicException('bar');
|
||||||
|
$e2 = new \RuntimeException('foo', 0, $e);
|
||||||
|
$formatted = $formatter->format(array(
|
||||||
|
'exception' => $e2,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertGreaterThan(5, count($formatted['exception']['trace']));
|
||||||
|
$this->assertTrue(isset($formatted['exception']['previous']));
|
||||||
|
unset($formatted['exception']['trace'], $formatted['exception']['previous']);
|
||||||
|
|
||||||
|
$this->assertEquals(array(
|
||||||
|
'exception' => array(
|
||||||
|
'class' => get_class($e2),
|
||||||
|
'message' => $e2->getMessage(),
|
||||||
|
'file' => $e2->getFile().':'.$e2->getLine(),
|
||||||
|
)
|
||||||
|
), $formatted);
|
||||||
|
}
|
||||||
|
|
||||||
public function testBatchFormat()
|
public function testBatchFormat()
|
||||||
{
|
{
|
||||||
$formatter = new NormalizerFormatter('Y-m-d');
|
$formatter = new NormalizerFormatter('Y-m-d');
|
||||||
|
Reference in New Issue
Block a user