-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -64,6 +67,7 @@ public function __construct( | |
bool $bubble = true, | ||
array $verbosityLevelMap = [], | ||
private array $consoleFormatterOptions = [], | ||
private bool $interactiveOnly = false, | ||
) { | ||
parent::__construct(Level::Debug, $bubble); | ||
|
||
|
@@ -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 | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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(); | ||
|
@@ -173,4 +194,9 @@ private function updateLevel(): bool | |
|
||
return true; | ||
} | ||
|
||
private function isInteractiveOnlyEnabled(): bool | ||
{ | ||
return $this->interactiveOnly && $this->input && $this->input->isInteractive(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.