mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 19:57:41 +02:00
Change the way objects are normalized to avoid multi-levels of json encoding, fixes #560
This commit is contained in:
@@ -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).')]';
|
||||
|
@@ -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',
|
||||
);
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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',
|
||||
|
Reference in New Issue
Block a user