1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-02 11:20:31 +02:00

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

This commit is contained in:
Jordi Boggiano
2016-05-26 19:21:47 +01:00
parent 3d7842ec1e
commit 2ff7afda31
5 changed files with 23 additions and 17 deletions

View File

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

View File

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

View File

@@ -117,7 +117,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: 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() public function testDefFormatWithException()
@@ -210,7 +210,7 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
class TestFoo class TestFoo
{ {
public $foo = 'foo'; public $foo = 'fooValue';
} }
class TestBar class TestBar

View File

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

View File

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