From 6586425a31ef18e3eb524293151be5bad9efbdf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nerijus=20Eimanavi=C4=8Dius?= Date: Sun, 28 Aug 2016 00:47:02 +0300 Subject: [PATCH] Better support for PHP7 \Throwable --- src/Monolog/Formatter/JsonFormatter.php | 5 ++-- tests/Monolog/Formatter/JsonFormatterTest.php | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index a985e2ab..4b2be77d 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -12,6 +12,7 @@ namespace Monolog\Formatter; use Exception; +use Throwable; /** * Encodes whatever record data is passed to it as json @@ -152,7 +153,7 @@ class JsonFormatter extends NormalizerFormatter return $normalized; } - if ($data instanceof Exception) { + if ($data instanceof Exception || $data instanceof Throwable) { return $this->normalizeException($data); } @@ -170,7 +171,7 @@ class JsonFormatter extends NormalizerFormatter protected function normalizeException($e) { // 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)); } diff --git a/tests/Monolog/Formatter/JsonFormatterTest.php b/tests/Monolog/Formatter/JsonFormatterTest.php index df7e35de..f75b274b 100644 --- a/tests/Monolog/Formatter/JsonFormatterTest.php +++ b/tests/Monolog/Formatter/JsonFormatterTest.php @@ -119,4 +119,32 @@ class JsonFormatterTest extends TestCase } $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); } + + public function testDefFormatWithThrowable() + { + if (!class_exists('Error') || !is_subclass_of('Error', 'Throwable')) { + $this->markTestSkipped('Requires PHP >=7'); + } + + $formatter = new JsonFormatter(); + $throwable = new \Error('Foo'); + $dateTime = new \DateTime(); + $message = $formatter->format(array( + 'level_name' => 'CRITICAL', + 'channel' => 'core', + 'context' => array('exception' => $throwable), + 'datetime' => $dateTime, + 'extra' => array(), + 'message' => 'foobar', + )); + + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $path = substr(json_encode($throwable->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1); + $encodedDateTime = json_encode($dateTime, JSON_UNESCAPED_SLASHES); + } else { + $path = substr(json_encode($throwable->getFile()), 1, -1); + $encodedDateTime = json_encode($dateTime); + } + $this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"'.get_class($throwable).'","message":"'.$throwable->getMessage().'","code":'.$throwable->getCode().',"file":"'.$path.':'.$throwable->getLine().'"}},"datetime":'.$encodedDateTime.',"extra":[],"message":"foobar"}'."\n", $message); + } }