Skip to content

Commit c7e655d

Browse files
committed
[MonologBridge] Make ConsoleHandler not handle messages at SILENT verbosity
Signed-off-by: Quentin Devos <[email protected]>
1 parent 365fe14 commit c7e655d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ private function updateLevel(): bool
167167
$verbosity = $this->output->getVerbosity();
168168
if (isset($this->verbosityLevelMap[$verbosity])) {
169169
$this->setLevel($this->verbosityLevelMap[$verbosity]);
170+
} elseif (\defined('\Symfony\Component\Console\Output\OutputInterface::VERBOSITY_SILENT') && OutputInterface::VERBOSITY_SILENT === $verbosity) {
171+
return false;
170172
} else {
171173
$this->setLevel(Level::Debug);
172174
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,58 @@ public static function provideVerbosityMappingTests(): array
101101
];
102102
}
103103

104+
/**
105+
* @dataProvider provideHandleOrBubbleSilentTests
106+
*/
107+
public function testHandleOrBubbleSilent(int $verbosity, Level $level, bool $isHandling, bool $isWriting, 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);
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($isWriting ? $this->once() : $this->never())
127+
->method('doWrite')
128+
->with($log, false);
129+
$handler = new ConsoleHandler($realOutput, false, $map);
130+
131+
$infoRecord = RecordFactory::create($level, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
132+
$this->assertSame($isHandling, $handler->handle($infoRecord), 'The handler bubbled correctly when it did not output the message.');
133+
}
134+
135+
public static function provideHandleOrBubbleSilentTests(): array
136+
{
137+
// The VERBOSITY_SILENT const is not defined for Console below 7.2, but in that case, the code behaves as before
138+
if (!\defined('\Symfony\Component\Console\Output\OutputInterface::VERBOSITY_SILENT')) {
139+
return [
140+
[OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, true],
141+
[OutputInterface::VERBOSITY_NORMAL, Level::Info, false, false],
142+
];
143+
}
144+
145+
return [
146+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, false, false],
147+
[OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, true],
148+
[OutputInterface::VERBOSITY_NORMAL, Level::Info, false, false],
149+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
150+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, false, false, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
151+
[OutputInterface::VERBOSITY_SILENT, Level::Emergency, false, false],
152+
[OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
153+
];
154+
}
155+
104156
public function testVerbosityChanged()
105157
{
106158
$output = $this->createMock(OutputInterface::class);

0 commit comments

Comments
 (0)