Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,23 @@ public function handle(LogRecord $record): bool
{
// we have to update the logging level each time because the verbosity of the
// console output might have changed in the meantime (it is not immutable)
Comment on lines 100 to 101
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this outdated comment.

return $this->updateLevel() && parent::handle($record);
if (!$this->updateLevel()) {
return false;
}

// AbstractProcessingHandler::handle() reads the $bubble property directly
// instead of calling getBubble(), so the dynamic value computed by the
// override above never reaches Monolog's Logger propagation loop. Mirror
// it into the property for the duration of this call so that
// interactive_only actually prevents propagation as documented.
$previousBubble = $this->bubble;
$this->bubble = $this->getBubble();

try {
return parent::handle($record);
} finally {
$this->bubble = $previousBubble;
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to update the fix to be an alternative "more correct" fix if there's a better way, but this solution gets the test passing.

Copy link
Copy Markdown
Member

@HypeMC HypeMC May 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something that should be fixed on Monolog's side instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix submitted here: Seldaek/monolog#2031

Comment thread
philbates35 marked this conversation as resolved.
Outdated
}

public function setInput(InputInterface $input): void
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Monolog\Tests\Handler;

use Monolog\Handler\TestHandler;
use Monolog\Level;
use Monolog\Logger;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -262,6 +263,46 @@ public function testInteractiveOnly()
self::assertTrue($handler->getBubble(), '->getBubble returns true when input is not interactive and interactiveOnly is true');
}

public function testInteractiveOnlyPreventsPropagationWhenInteractive()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails without the fix, proving that propogation isn't stopped, which contradicts the docs and I believe the intended behaviour.

Runtime:       PHP 8.2.31
Configuration: /home/runner/work/symfony/symfony/phpunit.xml

............................F.................................... 65 / 88 ( 73%)
.......................                                           88 / 88 (100%)

Time: 00:00.928, Memory: 24.00 MB

There was 1 failure:

1) Symfony\Bridge\Monolog\Tests\Handler\ConsoleHandlerTest::testInteractiveOnlyPreventsPropagationWhenInteractive
sibling handler must not receive records when interactive_only is true and input is interactive
Failed asserting that true is false.

/home/runner/work/symfony/symfony/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php:280
/home/runner/work/symfony/symfony/.phpunit/phpunit-11.5-0/phpunit:104
/home/runner/work/symfony/symfony/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php:458
/home/runner/work/symfony/symfony/vendor/symfony/phpunit-bridge/bin/simple-phpunit:13

FAILURES!
Tests: 88, Assertions: 238, Failures: 1.

{
$interactiveInput = $this->createStub(InputInterface::class);
$interactiveInput->method('isInteractive')->willReturn(true);

$consoleHandler = new ConsoleHandler(interactiveOnly: true);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use named arguments in test, because they are not supported by our BC policy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed πŸ‘

$consoleHandler->setInput($interactiveInput);
$consoleHandler->setOutput(new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE));

$sibling = new TestHandler();
$logger = new Logger('test', [$consoleHandler, $sibling]);

$logger->warning('hello');

self::assertFalse(
$sibling->hasWarningRecords(),
'sibling handler must not receive records when interactive_only is true and input is interactive',
);
}

public function testInteractiveOnlyAllowsPropagationWhenNotInteractive()
{
$nonInteractiveInput = $this->createStub(InputInterface::class);
$nonInteractiveInput->method('isInteractive')->willReturn(false);

$consoleHandler = new ConsoleHandler(interactiveOnly: true);
$consoleHandler->setInput($nonInteractiveInput);
$consoleHandler->setOutput(new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE));

$sibling = new TestHandler();
$logger = new Logger('test', [$consoleHandler, $sibling]);

$logger->warning('hello');

self::assertTrue(
$sibling->hasWarningRecords(),
'sibling handler must still receive records when interactive_only is true but input is not interactive',
);
}

public function testNestedCommandsDoNotCloseHandler()
{
$output = new BufferedOutput();
Expand Down
Loading