diff --git a/.gitignore b/.gitignore index 2f2601ea..620cd6ce 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ config.php .buildpath .htaccess PHPCI/config.yml -cache \ No newline at end of file +cache +/loggerconfig.php \ No newline at end of file diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 30e6380d..fe5b8601 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -252,7 +252,7 @@ class Builder implements LoggerAwareInterface // The build is added to the context so the logger can use // details from it if required. - $context['build'] = $this; + $context['build'] = $this->build; foreach ($message as $item) { $this->logger->log($level, $item, $context); diff --git a/PHPCI/Command/DaemonCommand.php b/PHPCI/Command/DaemonCommand.php index d6458f2b..3962d248 100644 --- a/PHPCI/Command/DaemonCommand.php +++ b/PHPCI/Command/DaemonCommand.php @@ -10,8 +10,10 @@ namespace PHPCI\Command; +use Monolog\Logger; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -27,6 +29,17 @@ use PHPCI\BuildFactory; */ class DaemonCommand extends Command { + /** + * @var \Monolog\Logger + */ + protected $logger; + + public function __construct(Logger $logger, $name = null) + { + parent::__construct($name); + $this->logger = $logger; + } + protected function configure() { $this @@ -68,12 +81,14 @@ class DaemonCommand extends Command if (file_exists(PHPCI_DIR.'/daemon/daemon.pid')) { echo "Already started\n"; + $this->logger->warning("Daemon already started"); return "alreadystarted"; } $logfile = PHPCI_DIR."/daemon/daemon.log"; $cmd = "nohup %s/daemonise phpci:daemonise > %s 2>&1 &"; $command = sprintf($cmd, PHPCI_DIR, $logfile); + $this->logger->info("Daemon started"); exec($command); } @@ -82,12 +97,14 @@ class DaemonCommand extends Command if (!file_exists(PHPCI_DIR.'/daemon/daemon.pid')) { echo "Not started\n"; + $this->logger->warning("Can't stop daemon as not started"); return "notstarted"; } $cmd = "kill $(cat %s/daemon/daemon.pid)"; $command = sprintf($cmd, PHPCI_DIR); exec($command); + $this->logger->info("Daemon stopped"); unlink(PHPCI_DIR.'/daemon/daemon.pid'); } @@ -110,13 +127,4 @@ class DaemonCommand extends Command echo "Not running\n"; return "notrunning"; } - - /** - * Called when log entries are made in Builder / the plugins. - * @see \PHPCI\Builder::log() - */ - public function logCallback($log) - { - $this->output->writeln($log); - } } diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php index 2acf83dc..d635b8a2 100644 --- a/PHPCI/Command/RunCommand.php +++ b/PHPCI/Command/RunCommand.php @@ -36,6 +36,22 @@ class RunCommand extends Command */ protected $output; + /** + * @var Logger + */ + protected $logger; + + /** + * @param \Monolog\Logger $logger + * @param string $name + */ + public function __construct(Logger $logger, $name = null) + { + parent::__construct($name); + $this->logger = $logger; + } + + protected function configure() { $this @@ -50,21 +66,22 @@ 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( + $this->logger->pushHandler( new OutputLogHandler($this->output, Logger::INFO) ); } - $logger->pushProcessor(new LoggedBuildContextTidier()); + $this->logger->pushProcessor(new LoggedBuildContextTidier()); + + $this->logger->addInfo("Finding builds to process"); + $store = Factory::getStore('Build'); + $result = $store->getByStatus(0); + $this->logger->addInfo(sprintf("Found %d builds", count($result['items']))); + + $builds = 0; foreach ($result['items'] as $build) { $builds++; @@ -74,16 +91,18 @@ class RunCommand extends Command // Logging relevant to this build should be stored // against the build itself. $buildDbLog = new BuildDBLogHandler($build, Logger::INFO); - $logger->pushHandler($buildDbLog); + $this->logger->pushHandler($buildDbLog); - $builder = new Builder($build, $logger); + $builder = new Builder($build, $this->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); + $this->logger->popHandler($buildDbLog); } + $this->logger->addInfo("Finished processing builds"); + return $builds; } } diff --git a/PHPCI/Command/UpdateCommand.php b/PHPCI/Command/UpdateCommand.php index e5806025..855ca893 100644 --- a/PHPCI/Command/UpdateCommand.php +++ b/PHPCI/Command/UpdateCommand.php @@ -9,8 +9,10 @@ namespace PHPCI\Command; +use Monolog\Logger; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -23,6 +25,17 @@ use Symfony\Component\Console\Output\OutputInterface; */ class UpdateCommand extends Command { + /** + * @var \Monolog\Logger + */ + protected $logger; + + public function __construct(Logger $logger, $name = null) + { + parent::__construct($name); + $this->logger = $logger; + } + protected function configure() { $this diff --git a/PHPCI/Helper/LoggedBuildContextTidier.php b/PHPCI/Helper/LoggedBuildContextTidier.php index 2a8a5e72..a67dd88d 100644 --- a/PHPCI/Helper/LoggedBuildContextTidier.php +++ b/PHPCI/Helper/LoggedBuildContextTidier.php @@ -2,9 +2,10 @@ namespace PHPCI\Helper; +use PHPCI\Model\Build; -class LoggedBuildContextTidier{ - +class LoggedBuildContextTidier +{ function __invoke() { return $this->tidyLoggedBuildContext(func_get_arg(0)); diff --git a/PHPCI/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php new file mode 100644 index 00000000..014805f2 --- /dev/null +++ b/PHPCI/Helper/LoggerConfig.php @@ -0,0 +1,58 @@ +config = require_once($logConfigFilePath); + } + else { + $this->config = array(); + } + } + + /** + * Returns an instance of Monolog with all configured handlers + * added. The Monolog instance will be given $name. + * @param $name + * @return Logger + */ + public function GetFor($name) { + $handlers = $this->getHandlers(self::KEY_AlwaysLoaded); + $handlers = array_merge($handlers, $this->getHandlers($name)); + return new Logger($name, $handlers); + } + + protected function getHandlers($key) { + $handlers = array(); + + // They key is expected to either be an array or + // a callable function that returns an array + if (isset($this->config[$key])) { + if (is_callable($this->config[$key])) { + $handlers = call_user_func($this->config[$key]); + } + elseif(is_array($this->config[$key])) { + $handlers = $this->config[$key]; + } + } + return $handlers; + } + +} \ No newline at end of file diff --git a/console b/console index 3a56e7cd..fc639be4 100755 --- a/console +++ b/console @@ -19,10 +19,14 @@ use PHPCI\Command\InstallCommand; use PHPCI\Command\DaemonCommand; use Symfony\Component\Console\Application; +$loggerConfig = new \PHPCI\Helper\LoggerConfig(__DIR__ . "/loggerconfig.php"); + $application = new Application(); -$application->add(new RunCommand); + +$application->add(new RunCommand($loggerConfig->GetFor('RunCommand'))); $application->add(new InstallCommand); -$application->add(new UpdateCommand); +$application->add(new UpdateCommand($loggerConfig->GetFor('UpdateCommand'))); $application->add(new GenerateCommand); -$application->add(new DaemonCommand); +$application->add(new DaemonCommand($loggerConfig->GetFor('DaemonCommand'))); + $application->run(); diff --git a/loggerconfig.php.example b/loggerconfig.php.example new file mode 100644 index 00000000..dad42f17 --- /dev/null +++ b/loggerconfig.php.example @@ -0,0 +1,16 @@ + function() { + return array( + new \Monolog\Handler\StreamHandler('c:\temp\errors.log', \Monolog\Logger::ERROR), + ); + }, + + /** Loggers for the RunCommand */ + 'RunCommand' => function() { + return array( + new \Monolog\Handler\RotatingFileHandler('c:\temp\everything',3, \Monolog\Logger::INFO), + ); + } +); \ No newline at end of file diff --git a/public/install.php b/public/install.php index e08e647e..0b7df4c8 100644 --- a/public/install.php +++ b/public/install.php @@ -1,6 +1,6 @@