From 278c26f589fa41506e13aa70a15c065d193f04d2 Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Fri, 13 May 2016 20:26:50 +0200 Subject: [PATCH] [Console] Display errors in quiet mode --- .../Bridge/Monolog/Handler/ConsoleHandler.php | 7 ++-- .../Tests/Handler/ConsoleHandlerTest.php | 25 +++++++++++++- .../Console/Logger/ConsoleLogger.php | 4 ++- .../Tests/Logger/ConsoleLoggerTest.php | 33 +++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php b/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php index 592584ffa4..d5cf8df61a 100644 --- a/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php +++ b/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php @@ -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 { diff --git a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php index 6cb315967e..713171cbb9 100644 --- a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php @@ -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), diff --git a/src/Symfony/Component/Console/Logger/ConsoleLogger.php b/src/Symfony/Component/Console/Logger/ConsoleLogger.php index 1f7417ea5a..eabd12dcb4 100644 --- a/src/Symfony/Component/Console/Logger/ConsoleLogger.php +++ b/src/Symfony/Component/Console/Logger/ConsoleLogger.php @@ -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', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context))); + $output->writeln(sprintf('<%1$s>[%2$s] %3$s', $this->formatLevelMap[$level], $level, $this->interpolate($message, $context)), $this->verbosityLevelMap[$level]); } } diff --git a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php index c5eca2cafd..15a19c58ab 100644 --- a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php +++ b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php @@ -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), + ); + } }