diff options
-rw-r--r-- | config/cli.php | 29 | ||||
-rw-r--r-- | core/Console.php | 20 | ||||
-rw-r--r-- | core/Container/StaticContainer.php | 16 | ||||
-rw-r--r-- | core/Log.php | 1 |
4 files changed, 66 insertions, 0 deletions
diff --git a/config/cli.php b/config/cli.php new file mode 100644 index 0000000000..50c4d5d885 --- /dev/null +++ b/config/cli.php @@ -0,0 +1,29 @@ +<?php + +use Interop\Container\ContainerInterface; +use Monolog\Logger; +use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter; +use Symfony\Bridge\Monolog\Handler\ConsoleHandler; +use Symfony\Component\Console\Output\OutputInterface; + +return array( + + // Log + 'log.handlers' => array( + DI\link('Symfony\Bridge\Monolog\Handler\ConsoleHandler'), + ), + 'Symfony\Bridge\Monolog\Handler\ConsoleHandler' => DI\factory(function (ContainerInterface $c) { + // Override the default verbosity map to make it more verbose by default + $verbosityMap = array( + OutputInterface::VERBOSITY_NORMAL => Logger::INFO, + OutputInterface::VERBOSITY_VERBOSE => Logger::DEBUG, + OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::DEBUG, + OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG, + ); + $handler = new ConsoleHandler(null, true, $verbosityMap); + $handler->setFormatter(new ConsoleFormatter($c->get('log.console.format'), null, true, true)); + return $handler; + }), + 'log.console.format' => '%start_tag%%level_name% %extra.class%[%datetime%]%end_tag% %message% %context% %extra%' . PHP_EOL, + +); diff --git a/core/Console.php b/core/Console.php index d0268569c9..baf5e6dbef 100644 --- a/core/Console.php +++ b/core/Console.php @@ -8,7 +8,9 @@ */ namespace Piwik; +use Piwik\Container\StaticContainer; use Piwik\Plugin\Manager as PluginManager; +use Symfony\Bridge\Monolog\Handler\ConsoleHandler; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -29,12 +31,15 @@ class Console extends Application ); $this->getDefinition()->addOption($option); + + StaticContainer::loadCliConfig(); } public function doRun(InputInterface $input, OutputInterface $output) { $this->initPiwikHost($input); $this->initConfig($output); + $this->initLoggerOutput($output); try { self::initPlugins(); @@ -142,6 +147,21 @@ class Console extends Application } } + /** + * Register the console output into the logger. + * + * Ideally, this should be done automatically with events: + * @see http://symfony.com/fr/doc/current/components/console/events.html + * @see Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand() + * But it would require to install Symfony's Event Dispatcher. + */ + private function initLoggerOutput(OutputInterface $output) + { + /** @var ConsoleHandler $consoleLogHandler */ + $consoleLogHandler = StaticContainer::getContainer()->get('Symfony\Bridge\Monolog\Handler\ConsoleHandler'); + $consoleLogHandler->setOutput($output); + } + public static function initPlugins() { Plugin\Manager::getInstance()->loadActivatedPlugins(); diff --git a/core/Container/StaticContainer.php b/core/Container/StaticContainer.php index 96a3742317..81d7eefe04 100644 --- a/core/Container/StaticContainer.php +++ b/core/Container/StaticContainer.php @@ -28,6 +28,13 @@ class StaticContainer private static $container; /** + * Should we load the CLI config. + * + * @var bool + */ + private static $cli = false; + + /** * @return Container */ public static function getContainer() @@ -66,6 +73,10 @@ class StaticContainer // Global config $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php'); + if (self::$cli) { + $builder->addDefinitions(PIWIK_USER_PATH . '/config/cli.php'); + } + // User config if (file_exists(PIWIK_USER_PATH . '/config/config.php')) { $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php'); @@ -73,4 +84,9 @@ class StaticContainer return $builder->build(); } + + public static function loadCliConfig() + { + self::$cli = true; + } } diff --git a/core/Log.php b/core/Log.php index d22b88160b..33e09d88ec 100644 --- a/core/Log.php +++ b/core/Log.php @@ -280,6 +280,7 @@ class Log extends Singleton 'level' => $this->getMonologLevel($level), 'level_name' => self::getStringLevel($level), 'time' => new \DateTime(), + 'datetime' => new \DateTime(), 'extra' => array(), ); |