diff --git a/composer.json b/composer.json index 33a0c646..458ec4c4 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "forum": "https://discourse.laminas.dev" }, "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + }, "sort-packages": true }, "extra": { diff --git a/src/Formatter/Simple.php b/src/Formatter/Simple.php index 5a360d81..5c4c2d13 100644 --- a/src/Formatter/Simple.php +++ b/src/Formatter/Simple.php @@ -7,6 +7,7 @@ use Laminas\Log\Exception; use Traversable; +use function array_key_exists; use function count; use function is_array; use function is_string; @@ -65,7 +66,7 @@ public function format($event) $event = parent::format($event); foreach ($event as $name => $value) { - if ('extra' === $name && count($value)) { + if ('extra' === $name && is_array($value) && count($value)) { $value = $this->normalize($value); } elseif ('extra' === $name) { // Don't print an empty array @@ -75,7 +76,7 @@ public function format($event) } if ( - isset($event['extra']) && empty($event['extra']) + array_key_exists('extra', $event) && empty($event['extra']) && false !== strpos($this->format, '%extra%') ) { $output = rtrim($output, ' '); diff --git a/test/Formatter/SimpleTest.php b/test/Formatter/SimpleTest.php index d344bdf8..4e3f367e 100644 --- a/test/Formatter/SimpleTest.php +++ b/test/Formatter/SimpleTest.php @@ -4,11 +4,18 @@ namespace LaminasTest\Log\Formatter; +use ArrayIterator; use DateTime; +use EmptyIterator; use Laminas\Log\Exception\InvalidArgumentException; use Laminas\Log\Formatter\Simple; +use LaminasTest\Log\TestAsset\StringObject; use PHPUnit\Framework\TestCase; use RuntimeException; +use stdClass; + +use function fopen; +use function range; class SimpleTest extends TestCase { @@ -53,6 +60,82 @@ public function testDefaultFormat(): void $this->assertEquals($outputExpected, $formatter->format($fields)); } + public function testFormatAllTypes(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $object = new stdClass(); + $object->foo = 'bar'; + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => [ + 'float' => 0.2, + 'boolean' => false, + 'array_empty' => [], + 'array' => range(0, 4), + 'traversable_empty' => new EmptyIterator(), + 'traversable' => new ArrayIterator(['id', 42]), + 'null' => null, + 'object_empty' => new stdClass(), + 'object' => $object, + 'string object' => new StringObject(), + 'resource' => fopen('php://stdout', 'w'), + ], + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo {' + . '"float":0.2,' + . '"boolean":false,' + . '"array_empty":"[]",' + . '"array":"[0,1,2,3,4]",' + . '"traversable_empty":"[]",' + . '"traversable":"[\"id\",42]",' + . '"null":null,' + . '"object_empty":"object(stdClass) {}",' + . '"object":"object(stdClass) {\"foo\":\"bar\"}",' + . '"string object":"Hello World",' + . '"resource":"resource(stream)"}'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + + public function testFormatExtraArrayKeyWithNonArrayValue(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => '', + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + + public function testFormatExtraArrayKeyWithNullValue(): void + { + $date = new DateTime('2012-08-28T18:15:00Z'); + $fields = [ + 'timestamp' => $date, + 'message' => 'foo', + 'priority' => 42, + 'priorityName' => 'bar', + 'extra' => null, + ]; + + $outputExpected = '2012-08-28T18:15:00+00:00 bar (42): foo'; + $formatter = new Simple(); + + $this->assertEquals($outputExpected, $formatter->format($fields)); + } + /** * @dataProvider provideDateTimeFormats */