[GH-ISSUE #1640] PHPstan support for "formatted" value of record #697

Closed
opened 2026-03-04 02:17:10 +03:00 by kerem · 1 comment
Owner

Originally created by @sonklein on GitHub (Mar 4, 2022).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1640

Hello,

I'm currently trying to test my logs using the Monolog TestHandler (through symfony)
I wanted to see if my logs were correctly formatted as we use our own Formatter. When I dump my logs the record contains the "formatted" key and I can actually test it's value, it works fine.

My issue is when I try to pass PHPStan on it :
98 Call to static method PHPUnit\Framework\Assert::assertArrayHasKey() with 'formatted' and array('message' => string, 'context' => array, 'level' => 100|200|250|300|400|500|550|600, 'level_name' => 'ALERT'|'CRITICAL'|'DEBUG'|'EMERGENCY'|'ERROR'|'INFO'|'NOTICE'|'WARNING', 'channel' => string, 'datetime' => DateTimeImmutable, 'extra' => array) will always evaluate to false.

From my understanding, it misses something in TestCase.php :

     * @param mixed[] $context
     *
     * @return array Record
     *
     * @phpstan-param  Level $level
     * @phpstan-return Record
     */
    protected function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = []): array
    {
        return [
            'message' => (string) $message,
            'context' => $context,
            'level' => $level,
            'level_name' => Logger::getLevelName($level),
            'channel' => 'test',
            'datetime' => new DateTimeImmutable(true),
            'extra' => [],
           // HERE ? 
        ];
    }

Or is there any other way to get the formatted output ?
In the meantime I added a /* @phpstan-ignore-line */ next to my test

Originally created by @sonklein on GitHub (Mar 4, 2022). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1640 Hello, I'm currently trying to test my logs using the Monolog TestHandler (through symfony) I wanted to see if my logs were correctly formatted as we use our own Formatter. When I dump my logs the record contains the "formatted" key and I can actually test it's value, it works fine. My issue is when I try to pass PHPStan on it : ` 98 Call to static method PHPUnit\Framework\Assert::assertArrayHasKey() with 'formatted' and array('message' => string, 'context' => array, 'level' => 100|200|250|300|400|500|550|600, 'level_name' => 'ALERT'|'CRITICAL'|'DEBUG'|'EMERGENCY'|'ERROR'|'INFO'|'NOTICE'|'WARNING', 'channel' => string, 'datetime' => DateTimeImmutable, 'extra' => array) will always evaluate to false. ` From my understanding, it misses something in TestCase.php : ```php *** * @param mixed[] $context * * @return array Record * * @phpstan-param Level $level * @phpstan-return Record */ protected function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = []): array { return [ 'message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => Logger::getLevelName($level), 'channel' => 'test', 'datetime' => new DateTimeImmutable(true), 'extra' => [], // HERE ? ]; } ``` Or is there any other way to get the formatted output ? In the meantime I added a /* @phpstan-ignore-line */ next to my test
kerem 2026-03-04 02:17:10 +03:00
  • closed this issue
  • added the
    Feature
    label
Author
Owner

@Seldaek commented on GitHub (Mar 13, 2022):

This should hopefully become easier with Monolog 3.0 as the record is now an object, see https://github.com/Seldaek/monolog/pull/1635 for the WIP.

<!-- gh-comment-id:1066167392 --> @Seldaek commented on GitHub (Mar 13, 2022): This should hopefully become easier with Monolog 3.0 as the record is now an object, see https://github.com/Seldaek/monolog/pull/1635 for the WIP.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#697
No description provided.