Skip to content

Commit 0fbbc35

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

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
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,8 @@ public function __construct(
6465
bool $bubble = true,
6566
array $verbosityLevelMap = [],
6667
private array $consoleFormatterOptions = [],
68+
private ?InputInterface $input = null,
69+
private bool $interactiveOnly = false,
6770
) {
6871
parent::__construct(Level::Debug, $bubble);
6972

@@ -74,7 +77,16 @@ public function __construct(
7477

7578
public function isHandling(LogRecord $record): bool
7679
{
77-
return $this->updateLevel() && parent::isHandling($record);
80+
return $this->isInteractiveOnlyEnabled() || ($this->updateLevel() && parent::isHandling($record));
81+
}
82+
83+
public function getBubble(): bool
84+
{
85+
if ($this->isInteractiveOnlyEnabled()) {
86+
return false;
87+
}
88+
89+
return parent::getBubble();
7890
}
7991

8092
public function handle(LogRecord $record): bool
@@ -84,6 +96,11 @@ public function handle(LogRecord $record): bool
8496
return $this->updateLevel() && parent::handle($record);
8597
}
8698

99+
public function setInput(InputInterface $input): void
100+
{
101+
$this->input = $input;
102+
}
103+
87104
/**
88105
* Sets the console output to use for printing logs.
89106
*/
@@ -108,6 +125,9 @@ public function close(): void
108125
*/
109126
public function onCommand(ConsoleCommandEvent $event): void
110127
{
128+
$input = $event->getInput();
129+
$this->setInput($input);
130+
111131
$output = $event->getOutput();
112132
if ($output instanceof ConsoleOutputInterface) {
113133
$output = $output->getErrorOutput();
@@ -173,4 +193,9 @@ private function updateLevel(): bool
173193

174194
return true;
175195
}
196+
197+
private function isInteractiveOnlyEnabled(): bool
198+
{
199+
return $this->interactiveOnly && $this->input !== null && $this->input->isInteractive();
200+
}
176201
}

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

Lines changed: 22 additions & 0 deletions
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)