Description
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