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)
{
$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();

View file

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