From 16e4516415b23211882fbafa2112a690ce15d931 Mon Sep 17 00:00:00 2001 From: Adirelle Date: Tue, 28 Apr 2015 07:39:10 +0200 Subject: [PATCH 1/5] OutputLogHandler: filter messages according to verbosity level. --- PHPCI/Command/RunCommand.php | 9 +-------- PHPCI/Logging/OutputLogHandler.php | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php index c2c352e6..99d895dd 100644 --- a/PHPCI/Command/RunCommand.php +++ b/PHPCI/Command/RunCommand.php @@ -74,14 +74,7 @@ class RunCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { $this->output = $output; - - // For verbose mode we want to output all informational and above - // messages to the symphony output interface. - if ($input->hasOption('verbose') && $input->getOption('verbose')) { - $this->logger->pushHandler( - new OutputLogHandler($this->output, Logger::INFO) - ); - } + $this->logger->pushHandler(new OutputLogHandler($output)); $running = $this->validateRunningBuilds(); diff --git a/PHPCI/Logging/OutputLogHandler.php b/PHPCI/Logging/OutputLogHandler.php index 4b4c81a4..43735c1a 100644 --- a/PHPCI/Logging/OutputLogHandler.php +++ b/PHPCI/Logging/OutputLogHandler.php @@ -10,6 +10,7 @@ namespace PHPCI\Logging; use Monolog\Handler\AbstractProcessingHandler; +use Monolog\Logger; use Psr\Log\LogLevel; use Symfony\Component\Console\Output\OutputInterface; @@ -20,21 +21,28 @@ use Symfony\Component\Console\Output\OutputInterface; class OutputLogHandler extends AbstractProcessingHandler { /** + * Map verbosity levels to log levels. + * + * @var int[] + */ + static protected $levels = array( + OutputInterface::VERBOSITY_QUIET => Logger::ERROR, + OutputInterface::VERBOSITY_NORMAL => Logger::WARNING, + OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE, + OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO, + OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG, + ); + * @var OutputInterface */ protected $output; /** * @param OutputInterface $output - * @param bool|string $level - * @param bool $bubble */ - public function __construct( - OutputInterface $output, - $level = LogLevel::INFO, - $bubble = true - ) { - parent::__construct($level, $bubble); + public function __construct(OutputInterface $output) + { + parent::__construct(static::$levels[$output->getVerbosity()]); $this->output = $output; } From dc431762d4e59fb65bc65c3a325ff30b8508edec Mon Sep 17 00:00:00 2001 From: Adirelle Date: Tue, 28 Apr 2015 07:41:49 +0200 Subject: [PATCH 2/5] OutputLogHandler: send messages of severity ERROR or higher to stderr. --- PHPCI/Logging/OutputLogHandler.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/PHPCI/Logging/OutputLogHandler.php b/PHPCI/Logging/OutputLogHandler.php index 43735c1a..3990e84b 100644 --- a/PHPCI/Logging/OutputLogHandler.php +++ b/PHPCI/Logging/OutputLogHandler.php @@ -12,6 +12,7 @@ namespace PHPCI\Logging; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; use Psr\Log\LogLevel; +use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; /** @@ -52,6 +53,12 @@ class OutputLogHandler extends AbstractProcessingHandler */ protected function write(array $record) { - $this->output->writeln((string)$record['formatted']); + if ($record['level'] >= Logger::ERROR && $this->output instanceof ConsoleOutputInterface) { + $output = $this->output->getErrorOutput(); + } else { + $output = $this->output; + } + + $output->write($record['formatted']); } } From bd54ee99058c8e4fbf44b4916b537ef1353c248e Mon Sep 17 00:00:00 2001 From: Adirelle Date: Tue, 28 Apr 2015 07:44:42 +0200 Subject: [PATCH 3/5] OutputLogHandler: enhance the formatter. --- PHPCI/Logging/OutputLogHandler.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/PHPCI/Logging/OutputLogHandler.php b/PHPCI/Logging/OutputLogHandler.php index 3990e84b..a3774ed7 100644 --- a/PHPCI/Logging/OutputLogHandler.php +++ b/PHPCI/Logging/OutputLogHandler.php @@ -60,5 +60,18 @@ class OutputLogHandler extends AbstractProcessingHandler } $output->write($record['formatted']); + + /** + * Enable the enhancements of the default formatter. + * + * @return FormatterInterface + */ + protected function getDefaultFormatter() + { + $formatter = parent::getDefaultFormatter(); + $formatter->ignoreEmptyContextAndExtra(true); + $formatter->allowInlineLineBreaks(true); + $formatter->includeStacktraces(true); + return $formatter; } } From 3af507e7b7375a133420b066be876ef701904401 Mon Sep 17 00:00:00 2001 From: Adirelle Date: Tue, 28 Apr 2015 07:47:39 +0200 Subject: [PATCH 4/5] OutputLogHandler: severity-based coloring. --- PHPCI/Logging/OutputLogHandler.php | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/PHPCI/Logging/OutputLogHandler.php b/PHPCI/Logging/OutputLogHandler.php index a3774ed7..0726a299 100644 --- a/PHPCI/Logging/OutputLogHandler.php +++ b/PHPCI/Logging/OutputLogHandler.php @@ -9,6 +9,8 @@ namespace PHPCI\Logging; +use Monolog\Formatter\FormatterInterface; +use Monolog\Formatter\LineFormatter; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; use Psr\Log\LogLevel; @@ -34,6 +36,19 @@ class OutputLogHandler extends AbstractProcessingHandler OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG, ); + /** + * Map log levels to colors. + * + * @var string[] + */ + static protected $colors = array( + Logger::ERROR => 'red', + Logger::WARNING => 'yellow', + Logger::NOTICE => 'green', + Logger::INFO => 'white' + ); + + /** * @var OutputInterface */ protected $output; @@ -45,6 +60,7 @@ class OutputLogHandler extends AbstractProcessingHandler { parent::__construct(static::$levels[$output->getVerbosity()]); $this->output = $output; + $this->pushProcessor(array($this, 'addConsoleColor')); } /** @@ -60,6 +76,7 @@ class OutputLogHandler extends AbstractProcessingHandler } $output->write($record['formatted']); + } /** * Enable the enhancements of the default formatter. @@ -74,4 +91,22 @@ class OutputLogHandler extends AbstractProcessingHandler $formatter->includeStacktraces(true); return $formatter; } + + /** + * Add console coloring to the message. + * + * @param array $record + * @return array + */ + public function addConsoleColor($record) + { + foreach (static::$colors as $level => $color) { + if ($record['level'] >= $level) { + break; + } + } + + $record['message'] = sprintf('%s', $color, rtrim($record['message']), $color); + return $record; + } } From 125ed061e5465cc6d9c260dfd8dec95426873605 Mon Sep 17 00:00:00 2001 From: Adirelle Date: Sun, 3 May 2015 14:33:28 +0200 Subject: [PATCH 5/5] Added some tests. --- PHPCI/Logging/OutputLogHandler.php | 12 ++- Tests/PHPCI/Logging/OutputLogHandlerTest.php | 77 ++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 Tests/PHPCI/Logging/OutputLogHandlerTest.php diff --git a/PHPCI/Logging/OutputLogHandler.php b/PHPCI/Logging/OutputLogHandler.php index 0726a299..0493cfe7 100644 --- a/PHPCI/Logging/OutputLogHandler.php +++ b/PHPCI/Logging/OutputLogHandler.php @@ -39,13 +39,14 @@ class OutputLogHandler extends AbstractProcessingHandler /** * Map log levels to colors. * - * @var string[] + * @var array */ static protected $colors = array( Logger::ERROR => 'red', Logger::WARNING => 'yellow', Logger::NOTICE => 'green', - Logger::INFO => 'white' + // No color markup below NOTICE + Logger::INFO => false ); /** @@ -97,6 +98,8 @@ class OutputLogHandler extends AbstractProcessingHandler * * @param array $record * @return array + * + * @internal Used as a Processor. */ public function addConsoleColor($record) { @@ -106,7 +109,10 @@ class OutputLogHandler extends AbstractProcessingHandler } } - $record['message'] = sprintf('%s', $color, rtrim($record['message']), $color); + if ($color !== false) { + $record['message'] = sprintf('%s', $color, rtrim($record['message']), $color); + } + return $record; } } diff --git a/Tests/PHPCI/Logging/OutputLogHandlerTest.php b/Tests/PHPCI/Logging/OutputLogHandlerTest.php new file mode 100644 index 00000000..ce4b9518 --- /dev/null +++ b/Tests/PHPCI/Logging/OutputLogHandlerTest.php @@ -0,0 +1,77 @@ +prophesize('\Symfony\Component\Console\Output\OutputInterface'); + $output->getVerbosity()->willReturn($verbosity); + + $handler = new OutputLogHandler($output->reveal()); + + $this->assertEquals($level, $handler->getLevel()); + } + + + public function getVerbosityLevelMap() + { + return array( + array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR), + array(OutputInterface::VERBOSITY_NORMAL, Logger::WARNING), + array(OutputInterface::VERBOSITY_VERBOSE, Logger::NOTICE), + array(OutputInterface::VERBOSITY_VERY_VERBOSE, Logger::INFO), + array(OutputInterface::VERBOSITY_DEBUG, Logger::DEBUG), + ); + } + + public function testSendInfoToOutput() + { + $record = array('message' => 'FOO', 'level' => Logger::INFO); + + $output = $this->prophesize('\Symfony\Component\Console\Output\ConsoleOutputInterface'); + $output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG); + $output->getErrorOutput()->shouldNotBeCalled(); + $output->write('BAR')->shouldBeCalled(); + + $formatter = $this->prophesize('\Monolog\Formatter\FormatterInterface'); + $formatter->format($record)->willReturn('BAR'); + + $handler = new OutputLogHandler($output->reveal()); + $handler->setFormatter($formatter->reveal()); + + $handler->handle($record); + } + + public function testSendErrorsToStderr() + { + $record = array('message' => 'FOO', 'level' => Logger::ERROR); + + $error = $this->prophesize('\Symfony\Component\Console\Output\OutputInterface'); + $error->write('BAR')->shouldBeCalled(); + + $output = $this->prophesize('\Symfony\Component\Console\Output\ConsoleOutputInterface'); + $output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG); + $output->getErrorOutput()->willReturn($error); + + $formatter = $this->prophesize('\Monolog\Formatter\FormatterInterface'); + $formatter->format(array('message' => 'FOO', 'level' => Logger::ERROR))->willReturn('BAR'); + + $handler = new OutputLogHandler($output->reveal()); + $handler->setFormatter($formatter->reveal()); + + $handler->handle($record); + } +}