Thanks to visit codestin.com
Credit goes to github.com

Skip to content

When using Monolog, the command "messenger:consume scheduler_default -vv" stops displaying logs in console after the first RunCommandMessage consumed. #59660

Open
@arnaud-netinfluence

Description

@arnaud-netinfluence

Symfony version(s) affected

7.2

Description

Using monolog with a minimal configuration:

    monolog:
        handlers:
            console:
                type: console

The command bin/console messenger:consume scheduler_default -vv should display a pretty line of log in the console every time it consumes a message.
18:51:00 INFO [messenger] Received message Symfony\Component\Console\Messenger\RunCommandMessage ["class" => "Symfony\Component\Console\Messenger\RunCommandMessage"]

When a RunCommandMessage is consumed, it does display the log line and the command keeps running, but when subsequent RunCommandMessage are consumed, their code are processed correctly however no log message is displayed.

Without monolog, the log lines are all displayed well in the console.

How to reproduce

On a fresh Symfony installation with the monolog bundle installed :

    monolog:
        handlers:
            console:
                type: console

  • Define a simple command and add to it the attribute AsCronTask()
#[AsCommand(
    name: 'app:cron:test',
    description: 'Cron test command'
)]
#[AsCronTask('* * * * *')]
class CronTestCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        echo "bruh !";
        return Command::SUCCESS;
    }
}
  • Start the message consumer with this command : bin/console messenger:consume scheduler_default -vv

  • After two minutes, notice there is only one log line but two "bruh !" outputted in the console.

18:51:00 INFO      [messenger] Received message Symfony\Component\Console\Messenger\RunCommandMessage ["class" => "Symfony\Component\Console\Messenger\RunCommandMessage"]
bruh !bruh !

Possible Solution

It has to do with how Symfony\Bridge\Monolog\Handler\ConsoleHandler set and delete its $output property.
Not a solution but a hint: I have been able to get the logs display correctly by dirty-hacking ConsoleHandler this way:

    public function onCommand(ConsoleCommandEvent $event): void
    {

        $output = $event->getOutput();
        if ($output instanceof ConsoleOutputInterface) {
            $output = $output->getErrorOutput();
        }

+        if (is_null($this->output)) {
            $this->setOutput($output);
+        }

    }

    public function onTerminate(ConsoleTerminateEvent $event): void
    {
-           $this->close();
+       // $this->close();
    }

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions