1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Fix up #546 to json serialize correctly

This commit is contained in:
Jordi Boggiano
2015-06-01 21:42:42 +01:00
parent cbf17236e6
commit 1a1f506f0e
3 changed files with 10 additions and 7 deletions

View File

@@ -94,12 +94,15 @@ class NormalizerFormatter implements FormatterInterface
return $this->normalizeException($data); return $this->normalizeException($data);
} }
$objData = $this->toJson($data, true);; // non-serializable objects that implement __toString stringified
if(!$objData and method_exists($data, '__toString')) { if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
$objData = $data; $value = (string) $data;
} else {
// the rest is json-serialized in some way
$value = $this->toJson($data, true);
} }
return sprintf("[object] (%s: %s)", get_class($data), $objData); return sprintf("[object] (%s: %s)", get_class($data), $value);
} }
if (is_resource($data)) { if (is_resource($data)) {

View File

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

View File

@@ -41,7 +41,7 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
'datetime' => date('Y-m-d'), 'datetime' => date('Y-m-d'),
'extra' => array( 'extra' => array(
'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})', 'foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})',
'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: {})', 'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: bar)',
'baz' => array(), 'baz' => array(),
'res' => '[resource]', 'res' => '[resource]',
), ),