mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 12:47:39 +02:00
Merge remote-tracking branch 'eimanavicius/support-for-Throwable-in-JsonFormatter' into 1.x
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
namespace Monolog\Formatter;
|
namespace Monolog\Formatter;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes whatever record data is passed to it as json
|
* Encodes whatever record data is passed to it as json
|
||||||
@@ -152,7 +153,7 @@ class JsonFormatter extends NormalizerFormatter
|
|||||||
return $normalized;
|
return $normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($data instanceof Exception) {
|
if ($data instanceof Exception || $data instanceof Throwable) {
|
||||||
return $this->normalizeException($data);
|
return $this->normalizeException($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +171,7 @@ class JsonFormatter extends NormalizerFormatter
|
|||||||
protected function normalizeException($e)
|
protected function normalizeException($e)
|
||||||
{
|
{
|
||||||
// TODO 2.0 only check for Throwable
|
// TODO 2.0 only check for Throwable
|
||||||
if (!$e instanceof Exception && !$e instanceof \Throwable) {
|
if (!$e instanceof Exception && !$e instanceof Throwable) {
|
||||||
throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
|
throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -80,43 +80,104 @@ class JsonFormatterTest extends TestCase
|
|||||||
{
|
{
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$exception = new \RuntimeException('Foo');
|
$exception = new \RuntimeException('Foo');
|
||||||
$message = $formatter->format(array(
|
$formattedException = $this->formatException($exception);
|
||||||
'level_name' => 'CRITICAL',
|
|
||||||
'channel' => 'core',
|
|
||||||
'context' => array('exception' => $exception),
|
|
||||||
'datetime' => new \DateTime(),
|
|
||||||
'extra' => array(),
|
|
||||||
'message' => 'foobar',
|
|
||||||
));
|
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
$message = $this->formatRecordWithExceptionInContext($formatter, $exception);
|
||||||
$path = substr(json_encode($exception->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
|
|
||||||
} else {
|
$this->assertContextContainsFormattedException($formattedException, $message);
|
||||||
$path = substr(json_encode($exception->getFile()), 1, -1);
|
|
||||||
}
|
|
||||||
$this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"RuntimeException","message":"'.$exception->getMessage().'","code":'.$exception->getCode().',"file":"'.$path.':'.$exception->getLine().'"}},"datetime":'.json_encode(new \DateTime()).',"extra":[],"message":"foobar"}'."\n", $message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefFormatWithPreviousException()
|
public function testDefFormatWithPreviousException()
|
||||||
{
|
{
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?'));
|
$exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?'));
|
||||||
|
$formattedPrevException = $this->formatException($exception->getPrevious());
|
||||||
|
$formattedException = $this->formatException($exception, $formattedPrevException);
|
||||||
|
|
||||||
|
$message = $this->formatRecordWithExceptionInContext($formatter, $exception);
|
||||||
|
|
||||||
|
$this->assertContextContainsFormattedException($formattedException, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefFormatWithThrowable()
|
||||||
|
{
|
||||||
|
if (!class_exists('Error') || !is_subclass_of('Error', 'Throwable')) {
|
||||||
|
$this->markTestSkipped('Requires PHP >=7');
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatter = new JsonFormatter();
|
||||||
|
$throwable = new \Error('Foo');
|
||||||
|
$formattedThrowable = $this->formatException($throwable);
|
||||||
|
|
||||||
|
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
|
||||||
|
|
||||||
|
$this->assertContextContainsFormattedException($formattedThrowable, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $expected
|
||||||
|
* @param string $actual
|
||||||
|
*
|
||||||
|
* @internal param string $exception
|
||||||
|
*/
|
||||||
|
private function assertContextContainsFormattedException($expected, $actual)
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n",
|
||||||
|
$actual
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param JsonFormatter $formatter
|
||||||
|
* @param \Exception|\Throwable $exception
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
|
||||||
|
{
|
||||||
$message = $formatter->format(array(
|
$message = $formatter->format(array(
|
||||||
'level_name' => 'CRITICAL',
|
'level_name' => 'CRITICAL',
|
||||||
'channel' => 'core',
|
'channel' => 'core',
|
||||||
'context' => array('exception' => $exception),
|
'context' => array('exception' => $exception),
|
||||||
'datetime' => new \DateTime(),
|
'datetime' => null,
|
||||||
'extra' => array(),
|
'extra' => array(),
|
||||||
'message' => 'foobar',
|
'message' => 'foobar',
|
||||||
));
|
));
|
||||||
|
return $message;
|
||||||
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
|
||||||
$pathPrevious = substr(json_encode($exception->getPrevious()->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
|
|
||||||
$pathException = substr(json_encode($exception->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
|
|
||||||
} else {
|
|
||||||
$pathPrevious = substr(json_encode($exception->getPrevious()->getFile()), 1, -1);
|
|
||||||
$pathException = substr(json_encode($exception->getFile()), 1, -1);
|
|
||||||
}
|
}
|
||||||
$this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"RuntimeException","message":"'.$exception->getMessage().'","code":'.$exception->getCode().',"file":"'.$pathException.':'.$exception->getLine().'","previous":{"class":"LogicException","message":"'.$exception->getPrevious()->getMessage().'","code":'.$exception->getPrevious()->getCode().',"file":"'.$pathPrevious.':'.$exception->getPrevious()->getLine().'"}}},"datetime":'.json_encode(new \DateTime()).',"extra":[],"message":"foobar"}'."\n", $message);
|
|
||||||
|
/**
|
||||||
|
* @param \Exception|\Throwable $exception
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function formatExceptionFilePathWithLine($exception)
|
||||||
|
{
|
||||||
|
$options = 0;
|
||||||
|
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
||||||
|
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
|
||||||
|
}
|
||||||
|
$path = substr(json_encode($exception->getFile(), $options), 1, -1);
|
||||||
|
return $path . ':' . $exception->getLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Exception|\Throwable $exception
|
||||||
|
*
|
||||||
|
* @param null|string $previous
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function formatException($exception, $previous = null)
|
||||||
|
{
|
||||||
|
$formattedException =
|
||||||
|
'{"class":"' . get_class($exception) .
|
||||||
|
'","message":"' . $exception->getMessage() .
|
||||||
|
'","code":' . $exception->getCode() .
|
||||||
|
',"file":"' . $this->formatExceptionFilePathWithLine($exception) .
|
||||||
|
($previous ? '","previous":' . $previous : '"') .
|
||||||
|
'}';
|
||||||
|
return $formattedException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user