Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize call normalizeRecord if necessary #1906

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,7 @@
*/
public function format(LogRecord $record): string
{
$normalized = parent::format($record);

if (isset($normalized['context']) && $normalized['context'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['context']);
} else {
$normalized['context'] = new \stdClass;
}
}
if (isset($normalized['extra']) && $normalized['extra'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['extra']);
} else {
$normalized['extra'] = new \stdClass;
}
}
$normalized = $this->normalizeRecord($record);

return $this->toJson($normalized, true) . ($this->appendNewline ? "\n" : '');
}
Expand All @@ -117,14 +102,41 @@
return $this;
}

/**
* @return array<array|bool|float|int|stdClass|string|null>
*/
protected function normalizeRecord(LogRecord $record): array

Check failure on line 108 in src/Monolog/Formatter/JsonFormatter.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Monolog\Formatter\JsonFormatter::normalizeRecord() has invalid return type Monolog\Formatter\stdClass.

Check failure on line 108 in src/Monolog/Formatter/JsonFormatter.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Monolog\Formatter\JsonFormatter::normalizeRecord() return type has no value type specified in iterable type array.

Check failure on line 108 in src/Monolog/Formatter/JsonFormatter.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Return type (array<array|bool|float|int|Monolog\Formatter\stdClass|string|null>) of method Monolog\Formatter\JsonFormatter::normalizeRecord() should be covariant with return type (array<array|bool|float|int|string|null>) of method Monolog\Formatter\NormalizerFormatter::normalizeRecord()
{
$normalized = parent::normalizeRecord($record);

if (isset($normalized['context']) && $normalized['context'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['context']);
} else {
$normalized['context'] = new \stdClass;
}
}
if (isset($normalized['extra']) && $normalized['extra'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['extra']);
} else {
$normalized['extra'] = new \stdClass;
}
}

return $normalized;

Check failure on line 127 in src/Monolog/Formatter/JsonFormatter.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Monolog\Formatter\JsonFormatter::normalizeRecord() should return array<array|bool|float|int|Monolog\Formatter\stdClass|string|null> but returns array<array|bool|float|int|stdClass|string|null>.
}

/**
* Return a JSON-encoded array of records.
*
* @phpstan-param LogRecord[] $records
*/
protected function formatBatchJson(array $records): string
{
return $this->toJson($this->normalize($records), true);
$formatted = array_map(fn (LogRecord $record) => $this->normalizeRecord($record), $records);

return $this->toJson($formatted, true);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Monolog/Formatter/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function testFormatBatch()
$this->getRecord(Level::Warning),
$this->getRecord(Level::Debug),
];
$this->assertEquals(json_encode($records), $formatter->formatBatch($records));
$expected = array_map(fn (LogRecord $record) => json_encode($record->toArray(), JSON_FORCE_OBJECT), $records);
$this->assertEquals('['.implode(',', $expected).']', $formatter->formatBatch($records));
}

/**
Expand Down
Loading