Skip to content

Commit 91f04e9

Browse files
bug symfony#60055 [MonologBridge] Make ConsoleHandler not handle messages at SILENT verbosity (okhoshi)
This PR was merged into the 7.3 branch. Discussion ---------- [MonologBridge] Make `ConsoleHandler` not handle messages at SILENT verbosity | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | symfony#53632 | License | MIT <details><summary>Original description</summary> <p> Adding a new constructor parameter to ConsoleHandler to let it bubble messages when the output is set at Silent verbosity level (like when using `--silent` in the CLI). Messages are dropped by the ConsoleHandler down the line because of the verbosity, but they are considered as handled and so bubbling is interrupted if the handler is set with `$bubble = false`. The use-case is to have the messages being either printed by the ConsoleHandler (and so seen by the person running the CLI) or sent to the logging system by the next handlers, but not both. Tweaking the `$verbosityLevelMap` is not perfect because EMERGENCY level can never be marked as not handled. With this change, the behaviour is more consistent between Silent and Quiet verbosity levels. </p> </details> Messages are dropped by the ConsoleHandler down the line because of the verbosity, but they are considered as handled and so bubbling is interrupted if the handler is set with `$bubble = false`. The use-case is to have the messages being either printed by the ConsoleHandler (and so seen by the person running the CLI) or sent to the logging system by the next handlers, but not both. By not handling messages when the ConsoleHandler verbosity is set to silent, the behaviour is more consistent between, Silent and all the other verbosity levels. Commits ------- c7e655d [MonologBridge] Make `ConsoleHandler` not handle messages at SILENT verbosity
2 parents 037e66f + c7e655d commit 91f04e9

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)