mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 04:37:38 +02:00
Json formatter should always format context/extra as an object, fixes #1028
This commit is contained in:
@@ -64,7 +64,15 @@ class JsonFormatter extends NormalizerFormatter
|
|||||||
*/
|
*/
|
||||||
public function format(array $record): string
|
public function format(array $record): string
|
||||||
{
|
{
|
||||||
return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
|
$normalized = $this->normalize($record);
|
||||||
|
if (isset($normalized['context']) && $normalized['context'] === []) {
|
||||||
|
$normalized['context'] = new \stdClass;
|
||||||
|
}
|
||||||
|
if (isset($normalized['extra']) && $normalized['extra'] === []) {
|
||||||
|
$normalized['extra'] = new \stdClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->toJson($normalized, true) . ($this->appendNewline ? "\n" : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -38,11 +38,12 @@ class JsonFormatterTest extends TestCase
|
|||||||
{
|
{
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$record = $this->getRecord();
|
$record = $this->getRecord();
|
||||||
|
$record['context'] = $record['extra'] = new \stdClass;
|
||||||
$this->assertEquals(json_encode($record)."\n", $formatter->format($record));
|
$this->assertEquals(json_encode($record)."\n", $formatter->format($record));
|
||||||
|
|
||||||
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
|
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
|
||||||
$record = $this->getRecord();
|
$record = $this->getRecord();
|
||||||
$this->assertEquals('{"message":"test","context":[],"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record['datetime']->format('Y-m-d\TH:i:s.uP').'","extra":[]}', $formatter->format($record));
|
$this->assertEquals('{"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record['datetime']->format('Y-m-d\TH:i:s.uP').'","extra":{}}', $formatter->format($record));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,6 +72,7 @@ class JsonFormatterTest extends TestCase
|
|||||||
$this->getRecord(Logger::DEBUG),
|
$this->getRecord(Logger::DEBUG),
|
||||||
];
|
];
|
||||||
array_walk($expected, function (&$value, $key) {
|
array_walk($expected, function (&$value, $key) {
|
||||||
|
$value['context'] = $value['extra'] = new \stdClass;
|
||||||
$value = json_encode($value);
|
$value = json_encode($value);
|
||||||
});
|
});
|
||||||
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
|
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
|
||||||
@@ -160,7 +162,7 @@ class JsonFormatterTest extends TestCase
|
|||||||
private function assertContextContainsFormattedException($expected, $actual)
|
private function assertContextContainsFormattedException($expected, $actual)
|
||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n",
|
'{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":{},"message":"foobar"}'."\n",
|
||||||
$actual
|
$actual
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -37,7 +37,7 @@ class LogmaticHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/testToken {"message":"Critical write test","context":\[\],"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
$this->assertRegexp('/testToken {"message":"Critical write test","context":{},"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteBatchContent()
|
public function testWriteBatchContent()
|
||||||
@@ -53,7 +53,7 @@ class LogmaticHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
$this->assertRegexp('/testToken {"message":"test","context":\[\],"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
$this->assertRegexp('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createHandler()
|
private function createHandler()
|
||||||
|
Reference in New Issue
Block a user