Skip to content

Commit cfaf929

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

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-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 (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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,50 @@ public static function provideVerbosityMappingTests(): array
101101
];
102102
}
103103

104+
/**
105+
* @dataProvider provideHandleOrBubbleSilentTests
106+
*/
107+
public function testHandleOrBubbleSilent(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);
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);
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 provideHandleOrBubbleSilentTests(): array
136+
{
137+
return [
138+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true],
139+
[OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
140+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
141+
[OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [OutputInterface::VERBOSITY_SILENT => Level::Error]],
142+
[OutputInterface::VERBOSITY_SILENT, Level::Emergency, false, true],
143+
[OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
144+
];
145+
}
146+
147+
104148
public function testVerbosityChanged()
105149
{
106150
$output = $this->createMock(OutputInterface::class);

0 commit comments

Comments
 (0)