Skip to content

Commit 5ac3f1d

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

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

+20-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;
@@ -64,6 +65,7 @@ public function __construct(
6465
bool $bubble = true,
6566
array $verbosityLevelMap = [],
6667
private array $consoleFormatterOptions = [],
68+
private ?InputInterface $input = null,
6769
) {
6870
parent::__construct(Level::Debug, $bubble);
6971

@@ -74,7 +76,16 @@ public function __construct(
7476

7577
public function isHandling(LogRecord $record): bool
7678
{
77-
return $this->updateLevel() && parent::isHandling($record);
79+
return ($this->input !== null && $this->input->isInteractive()) || $this->updateLevel() && parent::isHandling($record);
80+
}
81+
82+
public function getBubble(): bool
83+
{
84+
if ($this->input !== null && $this->input->isInteractive()) {
85+
return false;
86+
}
87+
88+
return parent::getBubble();
7889
}
7990

8091
public function handle(LogRecord $record): bool
@@ -84,6 +95,11 @@ public function handle(LogRecord $record): bool
8495
return $this->updateLevel() && parent::handle($record);
8596
}
8697

98+
public function setInput(InputInterface $input): void
99+
{
100+
$this->input = $input;
101+
}
102+
87103
/**
88104
* Sets the console output to use for printing logs.
89105
*/
@@ -108,6 +124,9 @@ public function close(): void
108124
*/
109125
public function onCommand(ConsoleCommandEvent $event): void
110126
{
127+
$input = $event->getInput();
128+
$this->setInput($input);
129+
111130
$output = $event->getOutput();
112131
if ($output instanceof ConsoleOutputInterface) {
113132
$output = $output->getErrorOutput();

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();
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();
207+
$handler->setInput($nonInteractiveInput);
208+
self::assertFalse($handler->isHandling($message));
209+
self::assertTrue($handler->getBubble());
210+
}
189211
}

0 commit comments

Comments
 (0)