Adding two custom log handlers. One to link the logs to symphony console output to the logging and another to record build specific information in the DB.

This commit is contained in:
meadsteve 2013-10-26 16:11:46 +01:00
parent a38a18f0fd
commit 1989203635
3 changed files with 93 additions and 9 deletions

View file

@ -9,6 +9,10 @@
namespace PHPCI\Command;
use Monolog\Logger;
use PHPCI\Helper\BuildDBLogHandler;
use PHPCI\Helper\OutputLogHandler;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@ -26,6 +30,11 @@ use PHPCI\BuildFactory;
*/
class RunCommand extends Command
{
/**
* @var OutputInterface
*/
protected $output;
protected function configure()
{
$this
@ -40,26 +49,36 @@ class RunCommand extends Command
{
$this->output = $output;
$logger = new Logger("BuildLog");
$store = Factory::getStore('Build');
$result = $store->getByStatus(0);
$builds = 0;
// For verbose mode we want to output all informational and above
// messages to the symphony output interface.
if ($input->getOption('verbose')) {
$logger->pushHandler(
new OutputLogHandler($this->output, Logger::INFO)
);
}
foreach ($result['items'] as $build) {
$builds++;
$build = BuildFactory::getBuild($build);
if ($input->getOption('verbose')) {
$builder = new Builder($build, function ($log) {
$this->output->writeln($log);
});
} else {
$builder = new Builder($build, function () {
// Empty stub function.
});
}
// Logging relevant to this build should be stored
// against the build itself.
$buildDbLog = new BuildDBLogHandler($build, Logger::INFO);
$logger->pushHandler($buildDbLog);
$builder = new Builder($build, $logger);
$builder->execute();
// After execution we no longer want to record the information
// back to this specific build so the handler should be removed.
$logger->popHandler($buildDbLog);
}
return $builds;

View file

@ -0,0 +1,33 @@
<?php
namespace PHPCI\Helper;
use b8\Store;
use Monolog\Handler\AbstractProcessingHandler;
use PHPCI\Model\Build;
class BuildDBLogHandler extends AbstractProcessingHandler
{
/**
* @var Build
*/
protected $build;
protected $logValue;
function __construct(Build $build,
$level = LogLevel::INFO,
$bubble = true)
{
parent::__construct($level, $bubble);
$this->build = $build;
// We want to add to any existing saved log information.
$this->logValue = $build->getLog();
}
protected function write(array $record) {
$this->logValue .= (string) $record['formatted'];
$this->build->setLog($this->logValue);
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace PHPCI\Helper;
use Monolog\Handler\AbstractProcessingHandler;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Output\OutputInterface;
class OutputLogHandler extends AbstractProcessingHandler
{
/**
* @var OutputInterface
*/
protected $output;
function __construct(OutputInterface $output,
$level = LogLevel::INFO,
$bubble = true)
{
parent::__construct($level, $bubble);
$this->output = $output;
}
protected function write(array $record)
{
$this->output->writeln((string) $record['formatted']);
}
}