Skip to content

[MonologBridge] Add ability to react to console input being interactive or not #59955

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -52,6 +53,8 @@ final class ConsoleHandler extends AbstractProcessingHandler implements EventSub
OutputInterface::VERBOSITY_DEBUG => Level::Debug,
];

private ?InputInterface $input = null;

/**
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
* until the output is set, e.g. by using console events)
Expand All @@ -64,6 +67,7 @@ public function __construct(
bool $bubble = true,
array $verbosityLevelMap = [],
private array $consoleFormatterOptions = [],
private bool $interactiveOnly = false,
) {
parent::__construct(Level::Debug, $bubble);

Expand All @@ -74,7 +78,16 @@ public function __construct(

public function isHandling(LogRecord $record): bool
{
return $this->updateLevel() && parent::isHandling($record);
return $this->isInteractiveOnlyEnabled() || ($this->updateLevel() && parent::isHandling($record));
}

public function getBubble(): bool
{
if ($this->isInteractiveOnlyEnabled()) {
return false;
}

return parent::getBubble();
}

public function handle(LogRecord $record): bool
Expand All @@ -84,6 +97,11 @@ public function handle(LogRecord $record): bool
return $this->updateLevel() && parent::handle($record);
}

public function setInput(InputInterface $input): void
{
$this->input = $input;
}

/**
* Sets the console output to use for printing logs.
*/
Expand All @@ -108,6 +126,9 @@ public function close(): void
*/
public function onCommand(ConsoleCommandEvent $event): void
{
$input = $event->getInput();
$this->setInput($input);

$output = $event->getOutput();
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
Expand Down Expand Up @@ -173,4 +194,9 @@ private function updateLevel(): bool

return true;
}

private function isInteractiveOnlyEnabled(): bool
{
return $this->interactiveOnly && $this->input && $this->input->isInteractive();
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need the interactiveOnly flag? Is determining if the console is interactive via the input not enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The input will always be injected, but you opt in to "interactive only" mode via config, see #58715 for an example, it would be a BC break without it.

}
}
22 changes: 22 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,26 @@ public function testLogsFromListeners()
$this->assertStringContainsString('Before terminate message.', $out = $output->fetch());
$this->assertStringContainsString('After terminate message.', $out);
}

public function testInteractiveOnly()
{
$message = RecordFactory::create(Level::Info, 'My info message');
$interactiveInput = $this->createMock(InputInterface::class);
$interactiveInput
->method('isInteractive')
->willReturn(true);
$handler = new ConsoleHandler(interactiveOnly: true);
$handler->setInput($interactiveInput);
self::assertTrue($handler->isHandling($message));
self::assertFalse($handler->getBubble());

$nonInteractiveInput = $this->createMock(InputInterface::class);
$nonInteractiveInput
->method('isInteractive')
->willReturn(false);
$handler = new ConsoleHandler(interactiveOnly: true);
$handler->setInput($nonInteractiveInput);
self::assertFalse($handler->isHandling($message));
self::assertTrue($handler->getBubble());
}
}
Loading