Skip to content

Commit

Permalink
feature #18781 [Console] Display errors in quiet mode (multi-io)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jun 10, 2016
2 parents 7ed1e97 + 278c26f commit e188cd7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Expand Up @@ -49,6 +49,7 @@ class ConsoleHandler extends AbstractProcessingHandler implements EventSubscribe
* @var array
*/
private $verbosityLevelMap = array(
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
Expand Down Expand Up @@ -154,7 +155,8 @@ public static function getSubscribedEvents()
*/
protected function write(array $record)
{
$this->output->write((string) $record['formatted']);
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
}

/**
Expand All @@ -172,10 +174,11 @@ protected function getDefaultFormatter()
*/
private function updateLevel()
{
if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) {
if (null === $this->output) {
return false;
}

$verbosity = $this->output->getVerbosity();
if (isset($this->verbosityLevelMap[$verbosity])) {
$this->setLevel($this->verbosityLevelMap[$verbosity]);
} else {
Expand Down
25 changes: 24 additions & 1 deletion src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Expand Up @@ -55,12 +55,35 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map
$this->assertSame($isHandling, $handler->isHandling(array('level' => $level)),
'->isHandling returns correct value depending on console verbosity and log level'
);

// check that the handler actually outputs the record if it handles it
$levelName = Logger::getLevelName($level);

$realOutput = $this->getMock('Symfony\Component\Console\Output\Output', array('doWrite'));
$realOutput->setVerbosity($verbosity);
$realOutput
->expects($isHandling ? $this->once() : $this->never())
->method('doWrite')
->with("[2013-05-29 16:21:54] app.$levelName: My info message \n", false);
$handler = new ConsoleHandler($realOutput, true, $map);

$infoRecord = array(
'message' => 'My info message',
'context' => array(),
'level' => $level,
'level_name' => Logger::getLevelName($level),
'channel' => 'app',
'datetime' => new \DateTime('2013-05-29 16:21:54'),
'extra' => array(),
);
$this->assertFalse($handler->handle($infoRecord), 'The handler finished handling the log.');
}

public function provideVerbosityMappingTests()
{
return array(
array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR, false),
array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR, true),
array(OutputInterface::VERBOSITY_QUIET, Logger::WARNING, false),
array(OutputInterface::VERBOSITY_NORMAL, Logger::WARNING, true),
array(OutputInterface::VERBOSITY_NORMAL, Logger::NOTICE, false),
array(OutputInterface::VERBOSITY_VERBOSE, Logger::NOTICE, true),
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Logger/ConsoleLogger.php
Expand Up @@ -88,8 +88,10 @@ public function log($level, $message, array $context = array())
$output = $this->output;
}

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

Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php
Expand Up @@ -14,6 +14,7 @@
use Psr\Log\Test\LoggerInterfaceTest;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -55,4 +56,36 @@ public function getLogs()
{
return $this->output->getLogs();
}

/**
* @dataProvider provideOutputMappingParams
*/
public function testOutputMapping($logLevel, $outputVerbosity, $isOutput, $addVerbosityLevelMap = array())
{
$out = new BufferedOutput($outputVerbosity);
$logger = new ConsoleLogger($out, $addVerbosityLevelMap);
$logger->log($logLevel, 'foo bar');
$logs = $out->fetch();
$this->assertEquals($isOutput ? "[$logLevel] foo bar\n" : '', $logs);
}

public function provideOutputMappingParams()
{
$quietMap = array(LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET);

return array(
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_NORMAL, true),
array(LogLevel::WARNING, OutputInterface::VERBOSITY_NORMAL, true),
array(LogLevel::INFO, OutputInterface::VERBOSITY_NORMAL, false),
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_NORMAL, false),
array(LogLevel::INFO, OutputInterface::VERBOSITY_VERBOSE, false),
array(LogLevel::INFO, OutputInterface::VERBOSITY_VERY_VERBOSE, true),
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE, false),
array(LogLevel::DEBUG, OutputInterface::VERBOSITY_DEBUG, true),
array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false),
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, false),
array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false, $quietMap),
array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
);
}
}

0 comments on commit e188cd7

Please sign in to comment.