[Console] Display errors in quiet mode
This commit is contained in:
parent
bcdf568a19
commit
278c26f589
@ -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,
|
||||
@ -154,7 +155,8 @@ class ConsoleHandler extends AbstractProcessingHandler implements EventSubscribe
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -172,10 +174,11 @@ class ConsoleHandler extends AbstractProcessingHandler implements EventSubscribe
|
||||
*/
|
||||
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 {
|
||||
|
@ -55,12 +55,35 @@ class ConsoleHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
$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),
|
||||
|
@ -88,8 +88,10 @@ class ConsoleLogger extends AbstractLogger
|
||||
$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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\Console\Tests\Logger;
|
||||
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;
|
||||
|
||||
@ -55,4 +56,36 @@ class ConsoleLoggerTest extends LoggerInterfaceTest
|
||||
{
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user