OutputLogHandler: filter messages according to verbosity level.

This commit is contained in:
aliaxander 2016-02-19 21:04:12 +03:00
parent 69f8f55836
commit 1bd24d3a57
2 changed files with 19 additions and 18 deletions

View file

@ -74,14 +74,7 @@ class RunCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->output = $output; $this->output = $output;
$this->logger->pushHandler(new OutputLogHandler($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)
);
}
$running = $this->validateRunningBuilds(); $running = $this->validateRunningBuilds();

View file

@ -10,40 +10,48 @@
namespace PHPCI\Logging; namespace PHPCI\Logging;
use Monolog\Handler\AbstractProcessingHandler; use Monolog\Handler\AbstractProcessingHandler;
use Psr\Log\LogLevel; use Monolog\Logger;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
/** /**
* Class OutputLogHandler outputs the build log to the terminal. * Class OutputLogHandler outputs the build log to the terminal.
*
* @package PHPCI\Logging * @package PHPCI\Logging
*/ */
class OutputLogHandler extends AbstractProcessingHandler class OutputLogHandler extends AbstractProcessingHandler
{ {
protected static $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 * @var OutputInterface
*/ */
protected $output; protected $output;
/** /**
* OutputLogHandler constructor.
*
* @param OutputInterface $output * @param OutputInterface $output
* @param bool|string $level
* @param bool $bubble
*/ */
public function __construct( public function __construct(OutputInterface $output)
OutputInterface $output, {
$level = LogLevel::INFO, parent::__construct(static::$levels[$output->getVerbosity()]);
$bubble = true
) {
parent::__construct($level, $bubble);
$this->output = $output; $this->output = $output;
} }
/** /**
* Write a log entry to the terminal. * Write a log entry to the terminal.
*
* @param array $record * @param array $record
*/ */
protected function write(array $record) protected function write(array $record)
{ {
$this->output->writeln((string)$record['formatted']); $this->output->writeln((string) $record['formatted']);
} }
} }