2020use Symfony \Component \Console \ConsoleEvents ;
2121use Symfony \Component \Console \Event \ConsoleCommandEvent ;
2222use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
23+ use Symfony \Component \Console \Input \InputInterface ;
2324use Symfony \Component \Console \Output \ConsoleOutputInterface ;
2425use Symfony \Component \Console \Output \OutputInterface ;
2526use 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,19 @@ 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 && $ this ->input ->isInteractive ()));
85+ }
86+
87+ public function getBubble (): bool
88+ {
89+ if ($ this ->isInteractiveOnlyEnabled ()) {
90+ return false ;
91+ }
92+
93+ return parent ::getBubble ();
7894 }
7995
8096 public function handle (LogRecord $ record ): bool
@@ -84,6 +100,11 @@ public function handle(LogRecord $record): bool
84100 return $ this ->updateLevel () && parent ::handle ($ record );
85101 }
86102
103+ public function setInput (InputInterface $ input ): void
104+ {
105+ $ this ->input = $ input ;
106+ }
107+
87108 /**
88109 * Sets the console output to use for printing logs.
89110 */
@@ -108,6 +129,8 @@ public function close(): void
108129 */
109130 public function onCommand (ConsoleCommandEvent $ event ): void
110131 {
132+ $ this ->setInput ($ event ->getInput ());
133+
111134 $ output = $ event ->getOutput ();
112135 if ($ output instanceof ConsoleOutputInterface) {
113136 $ output = $ output ->getErrorOutput ();
@@ -175,4 +198,9 @@ private function updateLevel(): bool
175198
176199 return true ;
177200 }
201+
202+ private function isInteractiveOnlyEnabled (): bool
203+ {
204+ return $ this ->interactiveOnly && $ this ->input && $ this ->input ->isInteractive ();
205+ }
178206}
0 commit comments