From 0fc91f053ac258022525fde4b40d333942b5a7fc Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 2 Nov 2013 15:28:24 +0000 Subject: [PATCH 1/6] Added to the console so that external logging definitions are pulled in from a loggerconfig.php file if the file exists. --- .gitignore | 3 ++- PHPCI/Helper/LoggerConfig.php | 51 +++++++++++++++++++++++++++++++++++ console | 6 ++++- loggerconfig.php.example | 9 +++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 PHPCI/Helper/LoggerConfig.php create mode 100644 loggerconfig.php.example 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/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php new file mode 100644 index 00000000..958f76eb --- /dev/null +++ b/PHPCI/Helper/LoggerConfig.php @@ -0,0 +1,51 @@ +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 = array(); + + // They key is expected to either be an array or + // a callable function that returns an array + if (isset($this->config[$name])) { + if (is_callable($this->config[$name])) { + $handlers = call_user_func($this->config[$name]); + } + elseif(is_array($this->config[$name])) { + $handlers = $this->config[$name]; + } + } + + return new Logger($name, $handlers); + } + +} \ No newline at end of file diff --git a/console b/console index 3a56e7cd..7525b054 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 GenerateCommand); $application->add(new DaemonCommand); + $application->run(); diff --git a/loggerconfig.php.example b/loggerconfig.php.example new file mode 100644 index 00000000..a5daccfe --- /dev/null +++ b/loggerconfig.php.example @@ -0,0 +1,9 @@ + function() { + return array( + new \Monolog\Handler\StreamHandler('path/to/logs/errors', \Monolog\Logger::ERROR), + new \Monolog\Handler\RotatingFileHandler('path/to/logs/everything',3, \Monolog\Logger::INFO), + ); + } +); \ No newline at end of file From 6563c60ab3e7d5ebd949dab98671b19dcb0a7fd6 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 2 Nov 2013 15:32:51 +0000 Subject: [PATCH 2/6] Previous commit omitted these changes. doh! --- PHPCI/Command/RunCommand.php | 41 ++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) 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; } } From 37c50bdb4c54dfdae6e2a2b45ff68e5581bad548 Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 2 Nov 2013 15:52:21 +0000 Subject: [PATCH 3/6] Added a logger config key that is loaded for all commands. To allow a general purpose log. --- PHPCI/Helper/LoggerConfig.php | 21 ++++++++++++++------- loggerconfig.php.example | 11 +++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/PHPCI/Helper/LoggerConfig.php b/PHPCI/Helper/LoggerConfig.php index 958f76eb..014805f2 100644 --- a/PHPCI/Helper/LoggerConfig.php +++ b/PHPCI/Helper/LoggerConfig.php @@ -8,6 +8,8 @@ use Monolog\Logger; class LoggerConfig { + const KEY_AlwaysLoaded = "_"; + private $config; /** @@ -32,20 +34,25 @@ class LoggerConfig { * @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[$name])) { - if (is_callable($this->config[$name])) { - $handlers = call_user_func($this->config[$name]); + if (isset($this->config[$key])) { + if (is_callable($this->config[$key])) { + $handlers = call_user_func($this->config[$key]); } - elseif(is_array($this->config[$name])) { - $handlers = $this->config[$name]; + elseif(is_array($this->config[$key])) { + $handlers = $this->config[$key]; } } - - return new Logger($name, $handlers); + return $handlers; } } \ No newline at end of file diff --git a/loggerconfig.php.example b/loggerconfig.php.example index a5daccfe..dad42f17 100644 --- a/loggerconfig.php.example +++ b/loggerconfig.php.example @@ -1,9 +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\StreamHandler('path/to/logs/errors', \Monolog\Logger::ERROR), - new \Monolog\Handler\RotatingFileHandler('path/to/logs/everything',3, \Monolog\Logger::INFO), + new \Monolog\Handler\RotatingFileHandler('c:\temp\everything',3, \Monolog\Logger::INFO), ); } ); \ No newline at end of file From a5957d687ac94fb1605bf0e19eb449a4893a4c9a Mon Sep 17 00:00:00 2001 From: meadsteve Date: Sat, 2 Nov 2013 15:52:55 +0000 Subject: [PATCH 4/6] Added logging to the daemon and update commands. --- PHPCI/Command/DaemonCommand.php | 26 +++++++++++++++++--------- PHPCI/Command/UpdateCommand.php | 13 +++++++++++++ console | 4 ++-- 3 files changed, 32 insertions(+), 11 deletions(-) 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/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/console b/console index 7525b054..fc639be4 100755 --- a/console +++ b/console @@ -25,8 +25,8 @@ $application = new Application(); $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(); From 217c29cac50a2d7efcc8666c28751f4277b3835f Mon Sep 17 00:00:00 2001 From: Thorsten Heymann Date: Tue, 5 Nov 2013 21:59:38 +0100 Subject: [PATCH 5/6] Fixed bug in install php. install.php requires bootstrap.php instead of vars.php because otherwise `$config` won't be set in vars.php. This fixes: Fatal error: Call to a member function get() on a non-object in /var/www/vars.php on line 11 --- public/install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 @@ Date: Tue, 12 Nov 2013 23:19:55 +0100 Subject: [PATCH 6/6] fixed LoggedBuildContextTidier: now correctly replaces "build" object with "buildID" in log entries --- PHPCI/Builder.php | 2 +- PHPCI/Helper/LoggedBuildContextTidier.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 11e46082..dc853fbf 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -249,7 +249,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/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));