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

Skip to content

[MonologBridge] Add $handleSilent constructor argument to ConsoleHandler #60055

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Monolog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `$handleSilent` constructor argument to `ConsoleHandler` to force the bubbling of message when output verbosity is set to `OutputInterface::VERBOSITY_SILENT`

7.0
---

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(
bool $bubble = true,
array $verbosityLevelMap = [],
private array $consoleFormatterOptions = [],
private bool $handleSilent = true,
) {
parent::__construct(Level::Debug, $bubble);

Expand Down Expand Up @@ -167,6 +168,8 @@ private function updateLevel(): bool
$verbosity = $this->output->getVerbosity();
if (isset($this->verbosityLevelMap[$verbosity])) {
$this->setLevel($this->verbosityLevelMap[$verbosity]);
} elseif (!$this->handleSilent && OutputInterface::VERBOSITY_SILENT === $verbosity) {
return false;
} else {
$this->setLevel(Level::Debug);
}
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,56 @@ public static function provideVerbosityMappingTests(): array
];
}

/**
* @dataProvider provideHandleSilentTests
*/
public function testHandleSilent(bool $silenced, int $verbosity, Level $level, bool $isHandling, bool $isBubbling, array $map = [])
{
$output = $this->createMock(OutputInterface::class);
$output
->expects($this->atLeastOnce())
->method('getVerbosity')
->willReturn($verbosity)
;
$handler = new ConsoleHandler($output, false, $map, handleSilent: $silenced);
$this->assertSame($isHandling, $handler->isHandling(RecordFactory::create($level)), '->isHandling returns correct value depending on console verbosity and log level');

// check that the handler actually outputs the record if it handles it at verbosity above SILENT
$levelName = Logger::getLevelName($level);
$levelName = \sprintf('%-9s', $levelName);

$realOutput = $this->getMockBuilder(Output::class)->onlyMethods(['doWrite'])->getMock();
$realOutput->setVerbosity($verbosity);
$log = "16:21:54 $levelName [app] My info message\n";
$realOutput
->expects($isHandling && $verbosity > OutputInterface::VERBOSITY_SILENT ? $this->once() : $this->never())
->method('doWrite')
->with($log, false);
$handler = new ConsoleHandler($realOutput, false, $map, handleSilent: $silenced);

$infoRecord = RecordFactory::create($level, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
$this->assertSame(!$isBubbling, $handler->handle($infoRecord), 'The handler bubbled correctly when it did not output the message.');
}

public static function provideHandleSilentTests(): array
{
return [
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false],
[true, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false],
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true],
[false, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, false, true],
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
];
}


public function testVerbosityChanged()
{
$output = $this->createMock(OutputInterface::class);
Expand Down
Loading