Skip to content

Commit ace9054

Browse files
dkarlovifabpot
authored andcommitted
[MonologBridge] Add ability to react to console input being interactive or not
1 parent a30f387 commit ace9054

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-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;
@@ -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,20 @@ public function __construct(
7478

7579
public function isHandling(LogRecord $record): bool
7680
{
77-
return $this->updateLevel() && parent::isHandling($record);
81+
return
82+
$this->updateLevel()
83+
&& parent::isHandling($record)
84+
&& (!$this->interactiveOnly || $this->input?->isInteractive())
85+
;
86+
}
87+
88+
public function getBubble(): bool
89+
{
90+
if ($this->interactiveOnly && $this->input?->isInteractive()) {
91+
return false;
92+
}
93+
94+
return parent::getBubble();
7895
}
7996

8097
public function handle(LogRecord $record): bool
@@ -84,6 +101,11 @@ public function handle(LogRecord $record): bool
84101
return $this->updateLevel() && parent::handle($record);
85102
}
86103

104+
public function setInput(InputInterface $input): void
105+
{
106+
$this->input = $input;
107+
}
108+
87109
/**
88110
* Sets the console output to use for printing logs.
89111
*/
@@ -97,6 +119,7 @@ public function setOutput(OutputInterface $output): void
97119
*/
98120
public function close(): void
99121
{
122+
$this->input = null;
100123
$this->output = null;
101124

102125
parent::close();
@@ -108,6 +131,8 @@ public function close(): void
108131
*/
109132
public function onCommand(ConsoleCommandEvent $event): void
110133
{
134+
$this->setInput($event->getInput());
135+
111136
$output = $event->getOutput();
112137
if ($output instanceof ConsoleOutputInterface) {
113138
$output = $output->getErrorOutput();

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,30 @@ public function testLogsFromListeners()
235235
$this->assertStringContainsString('Before terminate message.', $out = $output->fetch());
236236
$this->assertStringContainsString('After terminate message.', $out);
237237
}
238+
239+
public function testInteractiveOnly()
240+
{
241+
$output = $this->createMock(OutputInterface::class);
242+
243+
$message = RecordFactory::create(Level::Info, 'My info message');
244+
$interactiveInput = $this->createMock(InputInterface::class);
245+
$interactiveInput
246+
->method('isInteractive')
247+
->willReturn(true);
248+
$handler = new ConsoleHandler(interactiveOnly: true);
249+
$handler->setInput($interactiveInput);
250+
$handler->setOutput($output);
251+
self::assertTrue($handler->isHandling($message), '->isHandling returns true when input is interactive');
252+
self::assertFalse($handler->getBubble(), '->getBubble returns false when input is interactive and interactiveOnly is true');
253+
254+
$nonInteractiveInput = $this->createMock(InputInterface::class);
255+
$nonInteractiveInput
256+
->method('isInteractive')
257+
->willReturn(false);
258+
$handler = new ConsoleHandler(interactiveOnly: true);
259+
$handler->setInput($nonInteractiveInput);
260+
$handler->setOutput($output);
261+
self::assertFalse($handler->isHandling($message), '->isHandling returns false when input is not interactive');
262+
self::assertTrue($handler->getBubble(), '->getBubble returns true when input is not interactive and interactiveOnly is true');
263+
}
238264
}

0 commit comments

Comments
 (0)