2
0
Эх сурвалжийг харах

Change the way objects are normalized to avoid multi-levels of json encoding, fixes #560

Jordi Boggiano 9 жил өмнө
parent
commit
2ff7afda31

+ 12 - 6
src/Monolog/Formatter/NormalizerFormatter.php

@@ -98,19 +98,25 @@ class NormalizerFormatter implements FormatterInterface
                 return $this->normalizeException($data);
             }
 
-            // non-serializable objects that implement __toString stringified
-            if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
+            if ($data instanceof \JsonSerializable) {
+                $value = $data->jsonSerialize();
+            } elseif (method_exists($data, '__toString')) {
                 $value = $data->__toString();
             } else {
-                // the rest is json-serialized in some way
-                $value = $this->toJson($data, true);
+                // the rest is normalized by json encoding and decoding it
+                $encoded = $this->toJson($data, true);
+                if ($encoded === false) {
+                    $value = 'JSON_ERROR';
+                } else {
+                    $value = json_decode($encoded, true);
+                }
             }
 
-            return sprintf("[object] (%s: %s)", get_class($data), $value);
+            return [get_class($data) => $value];
         }
 
         if (is_resource($data)) {
-            return sprintf('[resource] (%s)', get_resource_type($data));
+            return sprintf('[resource(%s)]', get_resource_type($data));
         }
 
         return '[unknown('.gettype($data).')]';

+ 1 - 1
tests/Monolog/Formatter/ElasticaFormatterTest.php

@@ -44,7 +44,7 @@ class ElasticaFormatterTest extends \PHPUnit_Framework_TestCase
         $expected = $msg;
         $expected['datetime'] = '1970-01-01T00:00:00.000000+00:00';
         $expected['context'] = array(
-            'class' => '[object] (stdClass: {})',
+            'class' => ['stdClass' => []],
             'foo' => 7,
             0 => 'bar',
         );

+ 2 - 2
tests/Monolog/Formatter/LineFormatterTest.php

@@ -117,7 +117,7 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
             'message' => 'foobar',
         ));
 
-        $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: bar)","baz":[],"res":"[resource] (stream)"}'."\n", $message);
+        $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":{"Monolog\\\\Formatter\\\\TestFoo":{"foo":"fooValue"}},"bar":{"Monolog\\\\Formatter\\\\TestBar":"bar"},"baz":[],"res":"[resource(stream)]"}'."\n", $message);
     }
 
     public function testDefFormatWithException()
@@ -210,7 +210,7 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
 
 class TestFoo
 {
-    public $foo = 'foo';
+    public $foo = 'fooValue';
 }
 
 class TestBar

+ 7 - 7
tests/Monolog/Formatter/NormalizerFormatterTest.php

@@ -47,10 +47,10 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
             'message' => 'foo',
             'datetime' => date('Y-m-d'),
             'extra' => array(
-                'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
-                'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: bar)',
+                'foo' => ['Monolog\\Formatter\\TestFooNorm' => ["foo" => "fooValue"]],
+                'bar' => ['Monolog\\Formatter\\TestBarNorm' => 'bar'],
                 'baz' => array(),
-                'res' => '[resource] (stream)',
+                'res' => '[resource(stream)]',
             ),
             'context' => array(
                 'foo' => 'bar',
@@ -293,7 +293,7 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
             $wrappedResource->foo = $resource;
             // Just do something stupid with a resource/wrapped resource as argument
             array_keys($wrappedResource);
-        } catch (\Exception $e) {
+        } catch (\Throwable $e) {
             restore_error_handler();
         }
 
@@ -302,11 +302,11 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
         $result = $formatter->format($record);
 
         $this->assertRegExp(
-            '%"resource":"\[resource\] \(stream\)"%',
+            '%"resource":"\[resource\(stream\)\]"%',
             $result['context']['exception']['trace'][0]
         );
 
-        $pattern = '%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \)"%';
+        $pattern = '%"wrappedResource":\{"Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm":"JSON_ERROR"\}%';
 
         // Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
         $this->assertRegExp(
@@ -318,7 +318,7 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
 
 class TestFooNorm
 {
-    public $foo = 'foo';
+    public $foo = 'fooValue';
 }
 
 class TestBarNorm

+ 1 - 1
tests/Monolog/Handler/DoctrineCouchDBHandlerTest.php

@@ -34,7 +34,7 @@ class DoctrineCouchDBHandlerTest extends TestCase
 
         $expected = array(
             'message' => 'test',
-            'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
+            'context' => array('data' => ['stdClass' => []], 'foo' => 34),
             'level' => Logger::WARNING,
             'level_name' => 'WARNING',
             'channel' => 'test',