Skip to content

Commit 5dff435

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 5dff435

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-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 if 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 && $verbosity === OutputInterface::VERBOSITY_SILENT) {
172+
return false;
170173
} else {
171174
$this->setLevel(Level::Debug);
172175
}

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

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

104+
/**
105+
* @dataProvider provideHandleSilentTests
106+
*/
107+
public function testHandleSilent($handleSilent, $verbosity, $level, $isHandling, $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: $handleSilent);
116+
$this->assertSame($isHandling, $handler->isHandling(RecordFactory::create($level)),
117+
'->isHandling returns correct value depending on console verbosity and log level'
118+
);
119+
120+
// check that the handler actually outputs the record if it handles it at verbosity above SILENT
121+
$levelName = Logger::getLevelName($level);
122+
$levelName = \sprintf('%-9s', $levelName);
123+
124+
$realOutput = $this->getMockBuilder(Output::class)->onlyMethods(['doWrite'])->getMock();
125+
$realOutput->setVerbosity($verbosity);
126+
$log = "16:21:54 $levelName [app] My info message\n";
127+
$realOutput
128+
->expects($isHandling && $verbosity > OutputInterface::VERBOSITY_SILENT ? $this->once() : $this->never())
129+
->method('doWrite')
130+
->with($log, false);
131+
$handler = new ConsoleHandler($realOutput, false, $map, handleSilent: $handleSilent);
132+
133+
$infoRecord = RecordFactory::create($level, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
134+
$this->assertSame(!$isBubbling, $handler->handle($infoRecord),
135+
'The handler bubbled correctly when it did not output the message.'
136+
);
137+
}
138+
139+
public static function provideHandleSilentTests(): array
140+
{
141+
return [
142+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false],
143+
[true, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
144+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [
145+
OutputInterface::VERBOSITY_SILENT => Level::Warning,
146+
]],
147+
[true, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [
148+
OutputInterface::VERBOSITY_SILENT => Level::Error,
149+
]],
150+
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false],
151+
[true, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [
152+
OutputInterface::VERBOSITY_SILENT => Level::Emergency,
153+
]],
154+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true],
155+
[false, OutputInterface::VERBOSITY_NORMAL, Level::Warning, true, false],
156+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, true, false, [
157+
OutputInterface::VERBOSITY_SILENT => Level::Warning,
158+
]],
159+
[false, OutputInterface::VERBOSITY_SILENT, Level::Warning, false, true, [
160+
OutputInterface::VERBOSITY_SILENT => Level::Error,
161+
]],
162+
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, false, true],
163+
[false, OutputInterface::VERBOSITY_SILENT, Level::Emergency, true, false, [
164+
OutputInterface::VERBOSITY_SILENT => Level::Emergency,
165+
]],
166+
];
167+
}
168+
169+
104170
public function testVerbosityChanged()
105171
{
106172
$output = $this->createMock(OutputInterface::class);

0 commit comments

Comments
 (0)