Skip to content

Commit a827b61

Browse files
committed
[MonologBridge] Add $handleSilent constructor argument to ConsoleHandler
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
1 parent 7278aa0 commit a827b61

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Symfony/Bridge/Monolog/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `$handleSilent` constructor argument to `ConsoleHandler` to force the bubbling of message when output verbosity is set to `OutputInterface::VERBOSITY_SILENT`
8+
49
7.0
510
---
611

src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function __construct(
6464
bool $bubble = true,
6565
array $verbosityLevelMap = [],
6666
private array $consoleFormatterOptions = [],
67+
private bool $handleSilent = true,
6768
) {
6869
parent::__construct(Level::Debug, $bubble);
6970

@@ -167,6 +168,8 @@ private function updateLevel(): bool
167168
$verbosity = $this->output->getVerbosity();
168169
if (isset($this->verbosityLevelMap[$verbosity])) {
169170
$this->setLevel($this->verbosityLevelMap[$verbosity]);
171+
} elseif (!$this->handleSilent && OutputInterface::VERBOSITY_SILENT === $verbosity) {
172+
return false;
170173
} else {
171174
$this->setLevel(Level::Debug);
172175
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,56 @@ public static function provideVerbosityMappingTests(): array
100100
];
101101
}
102102

103+
/**
104+
* @dataProvider provideHandleSilentTests
105+
*/
106+
public function testHandleSilent(bool $silenced, int $verbosity, Level $level, bool $isHandling, bool $isBubbling, array $map = [])
107+
{
108+
$output = $this->createMock(OutputInterface::class);
109+
$output
110+
->expects($this->atLeastOnce())
111+
->method('getVerbosity')
112+
->willReturn($verbosity)
113+
;
114+
$handler = new ConsoleHandler($output, false, $map, handleSilent: $silenced);
115+
$this->assertSame($isHandling, $handler->isHandling(RecordFactory::create($level)), '->isHandling returns correct value depending on console verbosity and log level');
116+
117+
// check that the handler actually outputs the record if it handles it at verbosity above SILENT
118+
$levelName = Logger::getLevelName($level);
119+
$levelName = \sprintf('%-9s', $levelName);
120+
121+
$realOutput = $this->getMockBuilder(Output::class)->onlyMethods(['doWrite'])->getMock();
122+
$realOutput->setVerbosity($verbosity);
123+
$log = "16:21:54 $levelName [app] My info message\n";
124+
$realOutput
125+
->expects($isHandling && $verbosity > OutputInterface::VERBOSITY_SILENT ? $this->once() : $this->never())
126+
->method('doWrite')
127+
->with($log, false);
128+
$handler = new ConsoleHandler($realOutput, false, $map, handleSilent: $silenced);
129+
130+
$infoRecord = RecordFactory::create($level, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
131+
$this->assertSame(!$isBubbling, $handler->handle($infoRecord), 'The handler bubbled correctly when it did not output the message.');
132+
}
133+
134+
public static function provideHandleSilentTests(): array
135+
{
136+
return [
137+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false],
138+
[true, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
139+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
140+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
141+
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false],
142+
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
143+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true],
144+
[false, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
145+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
146+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
147+
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, false, true],
148+
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
149+
];
150+
}
151+
152+
103153
public function testVerbosityChanged()
104154
{
105155
$output = $this->createMock(OutputInterface::class);

0 commit comments

Comments
 (0)