8000 [Console] Display errors in quiet mode by multi-io · Pull Request #18781 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Display errors in quiet mode #18781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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