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

Enable JSON encode pretty print (#1236)

This commit is contained in:
Mponos George
2018-12-04 11:30:41 +02:00
committed by Jordi Boggiano
parent 204744df2e
commit daed05c3e5
3 changed files with 87 additions and 2 deletions

View File

@@ -46,6 +46,36 @@ class JsonFormatterTest extends TestCase
$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));
}
/**
* @covers Monolog\Formatter\JsonFormatter::format
*/
public function testFormatWithPrettyPrint()
{
$formatter = new JsonFormatter();
$formatter->setJsonPrettyPrint(true);
$record = $this->getRecord();
$record['context'] = $record['extra'] = new \stdClass;
$this->assertEquals(json_encode($record, JSON_PRETTY_PRINT)."\n", $formatter->format($record));
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
$formatter->setJsonPrettyPrint(true);
$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));
$formatter->setJsonPrettyPrint(false);
$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));
}
/**
* @covers Monolog\Formatter\JsonFormatter::formatBatch
* @covers Monolog\Formatter\JsonFormatter::formatBatchJson

View File

@@ -418,6 +418,45 @@ class NormalizerFormatterTest extends \PHPUnit\Framework\TestCase
);
}
/**
* This test was copied from `testExceptionTraceWithArgs` in order to ensure that pretty prints works
*/
public function testPrettyPrint()
{
try {
// This will contain $resource and $wrappedResource as arguments in the trace item
$resource = fopen('php://memory', 'rw+');
fwrite($resource, 'test_resource');
$wrappedResource = new TestFooNorm;
$wrappedResource->foo = $resource;
// Just do something stupid with a resource/wrapped resource as argument
$arr = [$wrappedResource, $resource];
// modifying the array inside throws a "usort(): Array was modified by the user comparison function"
usort($arr, function ($a, $b) {
throw new \ErrorException('Foo');
});
} catch (\Throwable $e) {
}
$formatter = new NormalizerFormatter();
$record = ['context' => ['exception' => $e]];
$formatter->setJsonPrettyPrint(true);
$result = $formatter->format($record);
$this->assertSame(
'{
"function": "Monolog\\\\Formatter\\\\{closure}",
"class": "Monolog\\\\Formatter\\\\NormalizerFormatterTest",
"type": "->",
"args": [
"[object] (Monolog\\\\Formatter\\\\TestFooNorm)",
"[resource(stream)]"
]
}',
$result['context']['exception']['trace'][0]
);
}
/**
* @param NormalizerFormatter $formatter
* @param \Throwable $exception