Skip to content

Commit c34db51

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

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
@@ -101,6 +101,56 @@ public static function provideVerbosityMappingTests(): array
101101
];
102102
}
103103

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

0 commit comments

Comments
 (0)