Skip to content

[FrameworkBundle] Add LoggerAssertionsTrait which provide shortcuts to assert a log has been written #51696

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

Closed

Conversation

welcoMattic
Copy link
Member

@welcoMattic welcoMattic commented Sep 19, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#...

Like #50662, this PR brings a new collection to enhance assertions to help users to ensure some logs has been written during a Request processing.

In a controller:

public function index(LoggerInterface $logger)
{
    $logger->debug('test1_' . __CLASS__);
    $logger->debug('test2_' . __CLASS__);
    $logger->debug('test3_' . __CLASS__);

    return new Response();
}

In the test:

public function testLoggerAssertion()
{
    $client = $this->createClient();
    $client->request('GET', '/log');

    $this->assertLogExists('test1_Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\LoggerController');
    $this->assertLogMatches("/(test2_).*(LoggerController)/");
    $this->assertLogContains('test3');
}

Todo

  • Write doc

@ismail1432
Copy link
Contributor

Good idea!

@welcoMattic welcoMattic changed the title [FrameworkBundle] Add LoggerAssertionsTrait which provide shortcuts to assert a log has been written [FrameworkBundle] Add LoggerAssertionsTrait which provide shortcuts to assert a log has been written Sep 21, 2023
@welcoMattic welcoMattic force-pushed the logger-profiler-assertions branch from efeddc0 to 41bc209 Compare September 21, 2023 10:05
Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be part of the Monolog bundle rather than the FrameworkBundle?
It does only work with Monolog

private static function ensureMonologHandlerIsAvailable(): void
{
if (!self::getContainer()->has('monolog.handler.test')) {
self::fail('The "monolog.handler.test" service is not available. Try registering the service "Monolog\Handler\TestHandler" as "monolog.handler.test" in your test configuration.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about mentioning using the Monolog bundle configuration if available?

when@test:
    monolog:
        handlers:
            test: { type: test }
            main:
                type: fingers_crossed
                # […]

This would work, because it'll be registered through the Monolog bundle extension in the handlers chain, but I don't get how just registering a monolog.handler.test would ensure this logger handler is used when using the logger in your application.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I should belongs to the Monolog bridge, as it only works with Monolog. Or I have to reimplement hasRecord* methods for generic loggers (which I do not want to do 😄).

I'm going to move it, thanks for your feedback

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't well know this part, especially how to use this bridge in tests. Would you help me?


trait LoggerAssertionsTrait
{
public static function assertLogExists(string $expectedLog, string $level = Logger::DEBUG): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature is wrong. Monolog's level constants on the Logger class are integers, not strings.

And they are deprecated in favor of the enum in 3.x.

and in 3.x, the various hasRecord* methods of theTestHandler expect the Level enum.

@nicolas-grekas nicolas-grekas added this to the 6.4 milestone Sep 25, 2023
@welcoMattic welcoMattic force-pushed the logger-profiler-assertions branch from 41bc209 to 34c2d5a Compare October 2, 2023 12:51
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@welcoMattic welcoMattic deleted the logger-profiler-assertions branch April 18, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants