Merge 125ed061e5
into 64b0f60368
This commit is contained in:
commit
e4f882738e
|
@ -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();
|
||||
|
||||
|
|
|
@ -9,8 +9,12 @@
|
|||
|
||||
namespace PHPCI\Logging;
|
||||
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Logger;
|
||||
use Psr\Log\LogLevel;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
|
@ -19,6 +23,32 @@ 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,
|
||||
);
|
||||
|
||||
/**
|
||||
* Map log levels to colors.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static protected $colors = array(
|
||||
Logger::ERROR => 'red',
|
||||
Logger::WARNING => 'yellow',
|
||||
Logger::NOTICE => 'green',
|
||||
// No color markup below NOTICE
|
||||
Logger::INFO => false
|
||||
);
|
||||
|
||||
/**
|
||||
* @var OutputInterface
|
||||
*/
|
||||
|
@ -26,16 +56,12 @@ class OutputLogHandler extends AbstractProcessingHandler
|
|||
|
||||
/**
|
||||
* @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;
|
||||
$this->pushProcessor(array($this, 'addConsoleColor'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,6 +70,49 @@ 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']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add console coloring to the message.
|
||||
*
|
||||
* @param array $record
|
||||
* @return array
|
||||
*
|
||||
* @internal Used as a Processor.
|
||||
*/
|
||||
public function addConsoleColor($record)
|
||||
{
|
||||
foreach (static::$colors as $level => $color) {
|
||||
if ($record['level'] >= $level) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($color !== false) {
|
||||
$record['message'] = sprintf('<fg=%s>%s</fg=%s>', $color, rtrim($record['message']), $color);
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
|
|
77
Tests/PHPCI/Logging/OutputLogHandlerTest.php
Normal file
77
Tests/PHPCI/Logging/OutputLogHandlerTest.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\PHPCI\Logging;
|
||||
|
||||
use Monolog\Logger;
|
||||
use PHPCI\Logging\OutputLogHandler;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Generated by PHPUnit_SkeletonGenerator on 2015-05-03 at 14:16:42.
|
||||
*/
|
||||
class OutputLogHandlerTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getVerbosityLevelMap
|
||||
*/
|
||||
public function testLevelMatchVerbosity($verbosity, $level)
|
||||
{
|
||||
$output = $this->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' => '<fg=red>FOO</fg=red>', 'level' => Logger::ERROR))->willReturn('BAR');
|
||||
|
||||
$handler = new OutputLogHandler($output->reveal());
|
||||
$handler->setFormatter($formatter->reveal());
|
||||
|
||||
$handler->handle($record);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue