Skip to content

Commit

Permalink
Normalize call normalizeRecord if necessary (#1906)
Browse files Browse the repository at this point in the history
* Normalize call normalizeRecord if necessary

* Update patch to always go through format()

* Update JsonFormatterTest.php

* Fix implementation

* Fix test expectations

* Update JsonFormatter.php

---------

Co-authored-by: Jordi Boggiano <[email protected]>
  • Loading branch information
johndodev and Seldaek authored Nov 11, 2024
1 parent f43e3d5 commit 5d5da57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
46 changes: 29 additions & 17 deletions src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,7 @@ public function isAppendingNewlines(): bool
*/
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 @@ -115,14 +100,41 @@ public function includeStacktraces(bool $include = true): self
return $this;
}

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

Check failure on line 106 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 106 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 106 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<mixed>|bool|float|int|string|null>) of method Monolog\Formatter\NormalizerFormatter::normalizeRecord()

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

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

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

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

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

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

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

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

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<mixed>|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 125 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<mixed>|bool|float|int|stdClass|string|null>.

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

View workflow job for this annotation

GitHub Actions / PHPStan (latest)

Method Monolog\Formatter\JsonFormatter::normalizeRecord() should return array<array|bool|float|int|Monolog\Formatter\stdClass|string|null> but returns array<array<mixed>|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

0 comments on commit 5d5da57

Please sign in to comment.