Skip to content

Commit e188cd7

Browse files
committed
feature #18781 [Console] Display errors in quiet mode (multi-io)
This PR was squashed before being merged into the 3.2-dev branch (closes #18781). Discussion ---------- [Console] Display errors in quiet mode | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18767 | License | MIT | Doc PR | - map VERBOSITY_QUIET normally, rather than suppressing all output without override - ensure that we do write to the output if we've determined (via verbosityLevelMap) that we should About the second point, it seems ConsoleLogger has the same issue (https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Console/Logger/ConsoleLogger.php#L92), but since we're not using that, I haven't checked it. Commits ------- 278c26f [Console] Display errors in quiet mode
2 parents 7ed1e97 + 278c26f commit e188cd7

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ConsoleHandler extends AbstractProcessingHandler implements EventSubscribe
4949
* @var array
5050
*/
5151
private $verbosityLevelMap = array(
52+
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
5253
OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
5354
OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
5455
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
@@ -154,7 +155,8 @@ public static function getSubscribedEvents()
154155
*/
155156
protected function write(array $record)
156157
{
157-
$this->output->write((string) $record['formatted']);
158+
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
159+
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
158160
}
159161

160162
/**
@@ -172,10 +174,11 @@ protected function getDefaultFormatter()
172174
*/
173175
private function updateLevel()
174176
{
175-
if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) {
177+
if (null === $this->output) {
176178
return false;
177179
}
178180

181+
$verbosity = $this->output->getVerbosity();
179182
if (isset($this->verbosityLevelMap[$verbosity])) {
180183
$this->setLevel($this->verbosityLevelMap[$verbosity]);
181184
} else {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,35 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map
5555
$this->assertSame($isHandling, $handler->isHandling(array('level' => $level)),
5656
'->isHandling returns correct value depending on console verbosity and log level'
5757
);
58+
59+
// check that the handler actually outputs the record if it handles it
60+
$levelName = Logger::getLevelName($level);
61+
62+
$realOutput = $this->getMock('Symfony\Component\Console\Output\Output', array('doWrite'));
63+
$realOutput->setVerbosity($verbosity);
64+
$realOutput
65+
->expects($isHandling ? $this->once() : $this->never())
66+
->method('doWrite')
67+
->with("[2013-05-29 16:21:54] app.$levelName: My info message \n", false);
68+
$handler = new ConsoleHandler($realOutput, true, $map);
69+
70+
$infoRecord = array(
71+
'message' => 'My info message',
72+
'context' => array(),
73+
'level' => $level,
74+
'level_name' => Logger::getLevelName($level),
75+
'channel' => 'app',
76+
'datetime' => new \DateTime('2013-05-29 16:21:54'),
77+
'extra' => array(),
78+
);
79+
$this->assertFalse($handler->handle($infoRecord), 'The handler finished handling the log.');
5880
}
5981

6082
public function provideVerbosityMappingTests()
6183
{
6284
return array(
63-
array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR, false),
85+
array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR, true),
86+
array(OutputInterface::VERBOSITY_QUIET, Logger::WARNING, false),
6487
array(OutputInterface::VERBOSITY_NORMAL, Logger::WARNING, true),
6588
array(OutputInterface::VERBOSITY_NORMAL, Logger::NOTICE, false),
6689
array(OutputInterface::VERBOSITY_VERBOSE, Logger::NOTICE, true),

src/Symfony/Component/Console/Logger/ConsoleLogger.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ public function log($level, $message, array $context = array())
8888
$output = $this->output;
8989
}
9090

91+
// the if condition check isn't necessary -- it's the same one that $output will do internally anyway.
92+
// We only do it for efficiency here as the message formatting is relatively expensive.
9193
if ($output->getVerbosity() >= $this->verbosityLevelMap[$level]) {
92-
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)));
94+
$output->writeln(sprintf('<%1$s>[%2$s] %3$s</%1$s>', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]);
9395
}
9496
}
9597

src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\Test\LoggerInterfaceTest;
1515
use Psr\Log\LogLevel;
1616
use Symfony\Component\Console\Logger\ConsoleLogger;
17+
use Symfony\Component\Console\Output\BufferedOutput;
1718
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920

@@ -55,4 +56,36 @@ public function getLogs()
5556
{
5657
return $this->output->getLogs();
5758
}
59+
60+
/**
61+
* @dataProvider provideOutputMappingParams
62+
*/
63+
public function testOutputMapping($logLevel, $outputVerbosity, $isOutput, $addVerbosityLevelMap = array())
64+
{
65+
$out = new BufferedOutput($outputVerbosity);
66+
$logger = new ConsoleLogger($out, $addVerbosityLevelMap);
67+
$logger->log($logLevel, 'foo bar');
68+
$logs = $out->fetch();
69+
$this->assertEquals($isOutput ? "[$logLevel] foo bar\n" : '', $logs);
70+
}
71+
72+
public function provideOutputMappingParams()
73+
{
74+
$quietMap = array(LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET);
75+
76+
return array(
77+
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_NORMAL, true),
78+
array(LogLevel::WARNING, OutputInterface::VERBOSITY_NORMAL, true),
79+
array(LogLevel::INFO, OutputInterface::VERBOSITY_NORMAL, false),
80+
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_NORMAL, false),
81+
array(LogLevel::INFO, OutputInterface::VERBOSITY_VERBOSE, false),
82+
array(LogLevel::INFO, OutputInterface::VERBOSITY_VERY_VERBOSE, true),
83+
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE, false),
84+
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_DEBUG, true),
85+
array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false),
86+
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, false),
87+
array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false, $quietMap),
88+
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
89+
);
90+
}
5891
}

0 commit comments

Comments
 (0)