From 6730e9210f807af2ba843c1c654974c648dacb6f Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Fri, 13 May 2016 20:26:50 +0200 Subject: [PATCH 1/6] bug #18767 ConsoleHandler output handling fixed - 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 --- .../Bridge/Monolog/Handler/ConsoleHandler.php | 7 +++-- .../Tests/Handler/ConsoleHandlerTest.php | 26 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php b/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php index 592584ffa4af0..d5cf8df61a897 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 @@ 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()); } /** @@ -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 { diff --git a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php index 6cb315967e4fc..31181897f8ba0 100644 --- a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php @@ -55,12 +55,36 @@ 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 iff it handles it + // (integration test for the issue #18767 fix) + $levelName = Logger::getLevelName($level); + + $realOutput = $this->getMock('Symfony\Component\Console\Output\Output', ['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), From 39c87bd83b313028c94e68b058894d79521d96e3 Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Thu, 19 May 2016 12:18:19 +0200 Subject: [PATCH 2/6] style fix --- src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php index 31181897f8ba0..e2389f2e26ab5 100644 --- a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php @@ -60,7 +60,7 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map // (integration test for the issue #18767 fix) $levelName = Logger::getLevelName($level); - $realOutput = $this->getMock('Symfony\Component\Console\Output\Output', ['doWrite']); + $realOutput = $this->getMock('Symfony\Component\Console\Output\Output', array('doWrite')); $realOutput->setVerbosity($verbosity); $realOutput ->expects($isHandling ? $this->once() : $this->never()) From d623402374e0e656d5c1bbc01c809e7bdcad3215 Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Sat, 21 May 2016 01:09:16 +0200 Subject: [PATCH 3/6] ConsoleLogger bugfix Previously, ConsoleLogger would always output everything to its backing output at the default verbosity (VERBOSITY_NORMAL), which meant that if the output had its verbosity setting set to VERBOSITY_QUIET, nothing would ever be output to it, even if the ConsoleLogger's verbosityMap specified that some logging levels should be output even to a VERBOSITY_QUIET output. --- .../Console/Logger/ConsoleLogger.php | 4 ++- .../Tests/Logger/ConsoleLoggerTest.php | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Logger/ConsoleLogger.php b/src/Symfony/Component/Console/Logger/ConsoleLogger.php index 1f7417ea5aa66..dd3c04bfe8349 100644 --- a/src/Symfony/Component/Console/Logger/ConsoleLogger.php +++ b/src/Symfony/Component/Console/Logger/ConsoleLogger.php @@ -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', $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 c5eca2cafdc07..cb0e623acbce4 100644 --- a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php +++ b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php @@ -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; @@ -55,4 +56,37 @@ 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), + ); + } + } From 6f153d79ddbcf7fc044b1093555d27897798f530 Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Thu, 9 Jun 2016 13:02:14 +0200 Subject: [PATCH 4/6] ConsoleLogger style fixes --- src/Symfony/Component/Console/Logger/ConsoleLogger.php | 4 ++-- .../Component/Console/Tests/Logger/ConsoleLoggerTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Logger/ConsoleLogger.php b/src/Symfony/Component/Console/Logger/ConsoleLogger.php index dd3c04bfe8349..eabd12dcb431c 100644 --- a/src/Symfony/Component/Console/Logger/ConsoleLogger.php +++ b/src/Symfony/Component/Console/Logger/ConsoleLogger.php @@ -88,8 +88,8 @@ 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. + // 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)), $this->verbosityLevelMap[$level]); } diff --git a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php index cb0e623acbce4..981d3c1422985 100644 --- a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php +++ b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php @@ -89,4 +89,4 @@ public function provideOutputMappingParams() ); } -} +} \ No newline at end of file From 0becfdf5123b97bbb9f88d6dcfec631d113986bb Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Thu, 9 Jun 2016 13:02:42 +0200 Subject: [PATCH 5/6] ConsoleHandler style fixes --- .../Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php index e2389f2e26ab5..713171cbb95d7 100644 --- a/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php @@ -56,8 +56,7 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map '->isHandling returns correct value depending on console verbosity and log level' ); - //check that the handler actually outputs the record iff it handles it - // (integration test for the issue #18767 fix) + // 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')); From 9d4adaace04e1cae7b3c986ea9cb089b2e8c6d8b Mon Sep 17 00:00:00 2001 From: Olaf Klischat Date: Thu, 9 Jun 2016 16:48:54 +0200 Subject: [PATCH 6/6] ConsoleLogger style fix --- .../Component/Console/Tests/Logger/ConsoleLoggerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php index 981d3c1422985..15a19c58abf09 100644 --- a/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php +++ b/src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php @@ -88,5 +88,4 @@ public function provideOutputMappingParams() array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap), ); } - -} \ No newline at end of file +}