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

If context has exactly 1000 items, do not truncate

This commit is contained in:
Minh-Quan TRAN
2017-08-17 17:11:16 +02:00
committed by Jordi Boggiano
parent 5f8783686e
commit ada5d30511
4 changed files with 60 additions and 4 deletions

View File

@@ -145,10 +145,11 @@ class JsonFormatter extends NormalizerFormatter
$count = 1; $count = 1;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if ($count++ >= 1000) { if ($count++ > 1000) {
$normalized['...'] = 'Over 1000 items, aborting normalization'; $normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value);
} }

View File

@@ -75,10 +75,11 @@ class NormalizerFormatter implements FormatterInterface
$count = 1; $count = 1;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if ($count++ >= 1000) { if ($count++ > 1000) {
$normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization'; $normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value);
} }

View File

@@ -180,4 +180,40 @@ class JsonFormatterTest extends TestCase
'}'; '}';
return $formattedException; return $formattedException;
} }
public function testNormalizeHandleLargeArraysWithExactly1000Items()
{
$formatter = new NormalizerFormatter();
$largeArray = range(1, 1000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertCount(1000, $res['context'][0]);
$this->assertArrayNotHasKey('...', $res['context'][0]);
}
public function testNormalizeHandleLargeArrays()
{
$formatter = new NormalizerFormatter();
$largeArray = range(1, 2000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertCount(1001, $res['context'][0]);
$this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
}
} }

View File

@@ -217,6 +217,24 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(@json_encode(array($resource)), $res); $this->assertEquals(@json_encode(array($resource)), $res);
} }
public function testNormalizeHandleLargeArraysWithExactly1000Items()
{
$formatter = new NormalizerFormatter();
$largeArray = range(1, 1000);
$res = $formatter->format(array(
'level_name' => 'CRITICAL',
'channel' => 'test',
'message' => 'bar',
'context' => array($largeArray),
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertCount(1000, $res['context'][0]);
$this->assertArrayNotHasKey('...', $res['context'][0]);
}
public function testNormalizeHandleLargeArrays() public function testNormalizeHandleLargeArrays()
{ {
$formatter = new NormalizerFormatter(); $formatter = new NormalizerFormatter();
@@ -231,7 +249,7 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
'extra' => array(), 'extra' => array(),
)); ));
$this->assertCount(1000, $res['context'][0]); $this->assertCount(1001, $res['context'][0]);
$this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']); $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
} }