Skip to content

Commit a17b916

Browse files
committed
[MonologBridge] Add ability to react to console input being interactive or not
1 parent 36092eb commit a17b916

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\ConsoleEvents;
2121
use Symfony\Component\Console\Event\ConsoleCommandEvent;
2222
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
23+
use Symfony\Component\Console\Input\InputInterface;
2324
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2425
use Symfony\Component\Console\Output\OutputInterface;
2526
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -52,6 +53,8 @@ final class ConsoleHandler extends AbstractProcessingHandler implements EventSub
5253
OutputInterface::VERBOSITY_DEBUG => Level::Debug,
5354
];
5455

56+
private ?InputInterface $input = null;
57+
5558
/**
5659
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
5760
* until the output is set, e.g. by using console events)
@@ -64,6 +67,7 @@ public function __construct(
6467
bool $bubble = true,
6568
array $verbosityLevelMap = [],
6669
private array $consoleFormatterOptions = [],
70+
private bool $interactiveOnly = false,
6771
) {
6872
parent::__construct(Level::Debug, $bubble);
6973

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

7579
public function isHandling(LogRecord $record): bool
7680
{
77-
return $this->updateLevel() && parent::isHandling($record);
81+
return $this->isInteractiveOnlyEnabled() || ($this->updateLevel() && parent::isHandling($record));
82+
}
83+
84+
public function getBubble(): bool
85+
{
86+
if ($this->isInteractiveOnlyEnabled()) {
87+
return false;
88+
}
89+
90+
return parent::getBubble();
7891
}
7992

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

100+
public function setInput(InputInterface $input): void
101+
{
102+
$this->input = $input;
103+
}
104+
87105
/**
88106
* Sets the console output to use for printing logs.
89107
*/
@@ -108,6 +126,9 @@ public function close(): void
108126
*/
109127
public function onCommand(ConsoleCommandEvent $event): void
110128
{
129+
$input = $event->getInput();
130+
$this->setInput($input);
131+
111132
$output = $event->getOutput();
112133
if ($output instanceof ConsoleOutputInterface) {
113134
$output = $output->getErrorOutput();
@@ -173,4 +194,9 @@ private function updateLevel(): bool
173194

174195
return true;
175196
}
197+
198+
private function isInteractiveOnlyEnabled(): bool
199+
{
200+
return $this->interactiveOnly && $this->input && $this->input->isInteractive();
201+
}
176202
}

src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,26 @@ public function testLogsFromListeners()
186186
$this->assertStringContainsString('Before terminate message.', $out = $output->fetch());
187187
$this->assertStringContainsString('After terminate message.', $out);
188188
}
189+
190+
public function testInteractiveOnly()
191+
{
192+
$message = RecordFactory::create(Level::Info, 'My info message');
193+
$interactiveInput = $this->createMock(InputInterface::class);
194+
$interactiveInput
195+
->method('isInteractive')
196+
->willReturn(true);
197+
$handler = new ConsoleHandler(interactiveOnly: true);
198+
$handler->setInput($interactiveInput);
199+
self::assertTrue($handler->isHandling($message));
200+
self::assertFalse($handler->getBubble());
201+
202+
$nonInteractiveInput = $this->createMock(InputInterface::class);
203+
$nonInteractiveInput
204+
->method('isInteractive')
205+
->willReturn(false);
206+
$handler = new ConsoleHandler(interactiveOnly: true);
207+
$handler->setInput($nonInteractiveInput);
208+
self::assertFalse($handler->isHandling($message));
209+
self::assertTrue($handler->getBubble());
210+
}
189211
}

0 commit comments

Comments
 (0)