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:
@@ -27,8 +27,6 @@ class JsonFormatter extends NormalizerFormatter
|
|||||||
|
|
||||||
protected $batchMode;
|
protected $batchMode;
|
||||||
protected $appendNewline;
|
protected $appendNewline;
|
||||||
protected $maxNormalizeDepth = 9;
|
|
||||||
protected $maxNormalizeItemCount = 1000;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
@@ -61,32 +59,6 @@ class JsonFormatter extends NormalizerFormatter
|
|||||||
return $this->appendNewline;
|
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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@@ -24,6 +24,8 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
const SIMPLE_DATE = "Y-m-d\TH:i:sP";
|
const SIMPLE_DATE = "Y-m-d\TH:i:sP";
|
||||||
|
|
||||||
protected $dateFormat;
|
protected $dateFormat;
|
||||||
|
protected $maxNormalizeDepth = 9;
|
||||||
|
protected $maxNormalizeItemCount = 1000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
|
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
|
||||||
@@ -56,14 +58,40 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
return $records;
|
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
|
* @param mixed $data
|
||||||
* @return int|bool|string|null|array
|
* @return int|bool|string|null|array
|
||||||
*/
|
*/
|
||||||
protected function normalize($data, int $depth = 0)
|
protected function normalize($data, int $depth = 0)
|
||||||
{
|
{
|
||||||
if ($depth > 9) {
|
if ($depth > $this->maxNormalizeDepth) {
|
||||||
return 'Over 9 levels deep, aborting normalization';
|
return 'Over ' . $this->maxNormalizeDepth . ' levels deep, aborting normalization';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $data || is_scalar($data)) {
|
if (null === $data || is_scalar($data)) {
|
||||||
@@ -84,8 +112,8 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
|
|
||||||
$count = 1;
|
$count = 1;
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
if ($count++ > 1000) {
|
if ($count++ > $this->maxNormalizeItemCount) {
|
||||||
$normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
|
$normalized['...'] = 'Over ' . $this->maxNormalizeItemCount . ' items ('.count($data).' total), aborting normalization';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -166,12 +166,12 @@ class JsonFormatterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param JsonFormatter $formatter
|
* @param JsonFormatter $formatter
|
||||||
* @param \Exception|\Throwable $exception
|
* @param \Throwable $exception
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
|
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, \Throwable $exception)
|
||||||
{
|
{
|
||||||
$message = $formatter->format([
|
$message = $formatter->format([
|
||||||
'level_name' => 'CRITICAL',
|
'level_name' => 'CRITICAL',
|
||||||
|
@@ -289,6 +289,48 @@ class NormalizerFormatterTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertSame('{"message":"€ŠšŽžŒœŸ"}', $res);
|
$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 $in Input
|
||||||
* @param mixed $expect Expected output
|
* @param mixed $expect Expected output
|
||||||
@@ -387,6 +429,26 @@ class NormalizerFormatterTest extends \PHPUnit\Framework\TestCase
|
|||||||
$result['context']['exception']['trace'][0]
|
$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
|
class TestFooNorm
|
||||||
|
Reference in New Issue
Block a user