Skip to content

Commit

Permalink
Merge branch 'fix/#39' into 2.15.x
Browse files Browse the repository at this point in the history
Fixes #39
Fixes #38
  • Loading branch information
Ocramius committed Jul 22, 2022
2 parents c46d9eb + 3526cbe commit 178b850
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"forum": "https://discourse.laminas.dev"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
},
"extra": {
Expand Down
5 changes: 3 additions & 2 deletions src/Formatter/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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, ' ');
Expand Down
83 changes: 83 additions & 0 deletions test/Formatter/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 178b850

Please sign in to comment.