From 1989203635979804a72a22f9e7506b195a11a28b Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 26 Oct 2013 16:11:46 +0100 Subject: [PATCH] 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. --- PHPCI/Command/RunCommand.php | 37 ++++++++++++++++++++++-------- PHPCI/Helper/BuildDBLogHandler.php | 33 ++++++++++++++++++++++++++ PHPCI/Helper/OutputLogHandler.php | 32 ++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 PHPCI/Helper/BuildDBLogHandler.php create mode 100644 PHPCI/Helper/OutputLogHandler.php diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php index dc8e21d2..26ef6c1e 100644 --- a/PHPCI/Command/RunCommand.php +++ b/PHPCI/Command/RunCommand.php @@ -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; diff --git a/PHPCI/Helper/BuildDBLogHandler.php b/PHPCI/Helper/BuildDBLogHandler.php new file mode 100644 index 00000000..c886767a --- /dev/null +++ b/PHPCI/Helper/BuildDBLogHandler.php @@ -0,0 +1,33 @@ +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); + } +} \ No newline at end of file diff --git a/PHPCI/Helper/OutputLogHandler.php b/PHPCI/Helper/OutputLogHandler.php new file mode 100644 index 00000000..7dd46d20 --- /dev/null +++ b/PHPCI/Helper/OutputLogHandler.php @@ -0,0 +1,32 @@ +output = $output; + } + + + protected function write(array $record) + { + $this->output->writeln((string) $record['formatted']); + } + + +} \ No newline at end of file