1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 18:30:15 +02:00

Migrate maxNormalizeDepth/maxNormalizeItemCount props and setters to NormalizerFormatter

This commit is contained in:
Jordi Boggiano
2018-06-08 23:09:04 +02:00
parent 518288c5c5
commit 800fe2cc1a
4 changed files with 97 additions and 35 deletions

View File

@@ -27,8 +27,6 @@ class JsonFormatter extends NormalizerFormatter
protected $batchMode;
protected $appendNewline;
protected $maxNormalizeDepth = 9;
protected $maxNormalizeItemCount = 1000;
/**
* @var bool
@@ -61,32 +59,6 @@ class JsonFormatter extends NormalizerFormatter
return $this->appendNewline;
}
/**
* The maximum number of normalization levels to go through
*/
public function getMaxNormalizeDepth(): int
{
return $this->maxNormalizeDepth;
}
public function setMaxNormalizeDepth(int $maxNormalizeDepth): void
{
$this->maxNormalizeDepth = $maxNormalizeDepth;
}
/**
* The maximum number of items to normalize per level
*/
public function getMaxNormalizeItemCount(): int
{
return $this->maxNormalizeItemCount;
}
public function setMaxNormalizeItemCount(int $maxNormalizeItemCount): void
{
$this->maxNormalizeItemCount = $maxNormalizeItemCount;
}
/**
* {@inheritdoc}
*/

View File

@@ -24,6 +24,8 @@ class NormalizerFormatter implements FormatterInterface
const SIMPLE_DATE = "Y-m-d\TH:i:sP";
protected $dateFormat;
protected $maxNormalizeDepth = 9;
protected $maxNormalizeItemCount = 1000;
/**
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
@@ -56,14 +58,40 @@ class NormalizerFormatter implements FormatterInterface
return $records;
}
/**
* The maximum number of normalization levels to go through
*/
public function getMaxNormalizeDepth(): int
{
return $this->maxNormalizeDepth;
}
public function setMaxNormalizeDepth(int $maxNormalizeDepth): void
{
$this->maxNormalizeDepth = $maxNormalizeDepth;
}
/**
* The maximum number of items to normalize per level
*/
public function getMaxNormalizeItemCount(): int
{
return $this->maxNormalizeItemCount;
}
public function setMaxNormalizeItemCount(int $maxNormalizeItemCount): void
{
$this->maxNormalizeItemCount = $maxNormalizeItemCount;
}
/**
* @param mixed $data
* @return int|bool|string|null|array
*/
protected function normalize($data, int $depth = 0)
{
if ($depth > 9) {
return 'Over 9 levels deep, aborting normalization';
if ($depth > $this->maxNormalizeDepth) {
return 'Over ' . $this->maxNormalizeDepth . ' levels deep, aborting normalization';
}
if (null === $data || is_scalar($data)) {
@@ -84,8 +112,8 @@ class NormalizerFormatter implements FormatterInterface
$count = 1;
foreach ($data as $key => $value) {
if ($count++ > 1000) {
$normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
if ($count++ > $this->maxNormalizeItemCount) {
$normalized['...'] = 'Over ' . $this->maxNormalizeItemCount . ' items ('.count($data).' total), aborting normalization';
break;
}

View File

@@ -167,11 +167,11 @@ class JsonFormatterTest extends TestCase
/**
* @param JsonFormatter $formatter
* @param \Exception|\Throwable $exception
* @param \Throwable $exception
*
* @return string
*/
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format([
'level_name' => 'CRITICAL',

View File

@@ -289,6 +289,48 @@ class NormalizerFormatterTest extends \PHPUnit\Framework\TestCase
$this->assertSame('{"message":"€ŠšŽžŒœŸ"}', $res);
}
public function testMaxNormalizeDepth()
{
$formatter = new NormalizerFormatter();
$formatter->setMaxNormalizeDepth(1);
$throwable = new \Error('Foo');
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
'Over 1 levels deep, aborting normalization',
$message['context']['exception']
);
}
public function testMaxNormalizeItemCountWith0ItemsMax()
{
$formatter = new NormalizerFormatter();
$formatter->setMaxNormalizeDepth(9);
$formatter->setMaxNormalizeItemCount(0);
$throwable = new \Error('Foo');
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
["..." => "Over 0 items (6 total), aborting normalization"],
$message
);
}
public function testMaxNormalizeItemCountWith3ItemsMax()
{
$formatter = new NormalizerFormatter();
$formatter->setMaxNormalizeDepth(9);
$formatter->setMaxNormalizeItemCount(2);
$throwable = new \Error('Foo');
$message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
$this->assertEquals(
["level_name" => "CRITICAL", "channel" => "core", "..." => "Over 2 items (6 total), aborting normalization"],
$message
);
}
/**
* @param mixed $in Input
* @param mixed $expect Expected output
@@ -387,6 +429,26 @@ class NormalizerFormatterTest extends \PHPUnit\Framework\TestCase
$result['context']['exception']['trace'][0]
);
}
/**
* @param NormalizerFormatter $formatter
* @param \Throwable $exception
*
* @return string
*/
private function formatRecordWithExceptionInContext(NormalizerFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format([
'level_name' => 'CRITICAL',
'channel' => 'core',
'context' => ['exception' => $exception],
'datetime' => null,
'extra' => [],
'message' => 'foobar',
]);
return $message;
}
}
class TestFooNorm