From 1bc8dfd5ebcc82fa09d120c507c83cee4aee43ee Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sun, 27 Aug 2017 00:10:28 +0700 Subject: [PATCH] Added daily rotate logger for console commands. Issue #108. --- docs/en/configuring.md | 3 + src/PHPCensor/Command/InstallCommand.php | 4 + src/PHPCensor/Console/Application.php | 44 +++++++--- src/PHPCensor/Logging/LoggerConfig.php | 71 ---------------- tests/PHPCensor/Logging/LoggerConfigTest.php | 86 -------------------- 5 files changed, 38 insertions(+), 170 deletions(-) delete mode 100644 src/PHPCensor/Logging/LoggerConfig.php delete mode 100644 tests/PHPCensor/Logging/LoggerConfigTest.php diff --git a/docs/en/configuring.md b/docs/en/configuring.md index 8ae10fca..62aba1e2 100644 --- a/docs/en/configuring.md +++ b/docs/en/configuring.md @@ -36,6 +36,9 @@ php-censor: host: localhost name: php-censor-queue lifetime: 600 + log: + rotate: true + max_files: 10 bitbucket: username: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx app_password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index efa11393..8f30efb2 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -253,6 +253,10 @@ class InstallCommand extends Command 'per_page' => 10, 'url' => $url, 'queue' => $queueConfig, + 'log' => [ + 'rotate' => false, + 'max_files' => 0, + ], 'email_settings' => [ 'from_address' => 'PHP Censor ', 'smtp_address' => null, diff --git a/src/PHPCensor/Console/Application.php b/src/PHPCensor/Console/Application.php index 0f4a0083..79d6fa26 100644 --- a/src/PHPCensor/Console/Application.php +++ b/src/PHPCensor/Console/Application.php @@ -4,6 +4,7 @@ namespace PHPCensor\Console; use b8\Config; use b8\Store\Factory; +use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\StreamHandler; use Monolog\Logger; use PHPCensor\Command\CreateAdminCommand; @@ -13,7 +14,7 @@ use PHPCensor\Command\RebuildCommand; use PHPCensor\Command\RebuildQueueCommand; use PHPCensor\Command\RunCommand; use PHPCensor\Command\WorkerCommand; -use PHPCensor\Logging\LoggerConfig; +use PHPCensor\Logging\Handler; use PHPCensor\Service\BuildService; use PHPCensor\Store\BuildStore; use PHPCensor\Store\ProjectStore; @@ -32,6 +33,29 @@ use Phinx\Config\Config as PhinxConfig; */ class Application extends BaseApplication { + /** + * @param Config $applicationConfig + * + * @return Logger + */ + protected function initLogger(Config $applicationConfig) + { + $rotate = (bool)$applicationConfig->get('php-censor.log.rotate', false); + $maxFiles = (int)$applicationConfig->get('php-censor.log.max_files', 0); + + $loggerHandlers = []; + if ($rotate) { + $loggerHandlers[] = new RotatingFileHandler(RUNTIME_DIR . 'console.log', $maxFiles, Logger::DEBUG); + } else { + $loggerHandlers[] = new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG); + } + + $logger = new Logger('php-censor', $loggerHandlers); + Handler::register($logger); + + return $logger; + } + /** * Constructor. * @@ -42,14 +66,6 @@ class Application extends BaseApplication { parent::__construct($name, $version); - $loggerConfig = new LoggerConfig([ - "_" => function() { - return [ - new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG), - ]; - } - ]); - $applicationConfig = Config::getInstance(); $databaseSettings = $applicationConfig->get('b8.database', []); @@ -109,12 +125,14 @@ class Application extends BaseApplication /** @var BuildStore $buildStore */ $buildStore = Factory::getStore('Build'); - $this->add(new RunCommand($loggerConfig->getFor('RunCommand'))); - $this->add(new RebuildCommand($loggerConfig->getFor('RunCommand'))); + $logger = $this->initLogger($applicationConfig); + + $this->add(new RunCommand($logger)); + $this->add(new RebuildCommand($logger)); $this->add(new InstallCommand()); $this->add(new CreateAdminCommand($userStore)); $this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore))); - $this->add(new WorkerCommand($loggerConfig->getFor('WorkerCommand'))); - $this->add(new RebuildQueueCommand($loggerConfig->getFor('RebuildQueueCommand'))); + $this->add(new WorkerCommand($logger)); + $this->add(new RebuildQueueCommand($logger)); } } diff --git a/src/PHPCensor/Logging/LoggerConfig.php b/src/PHPCensor/Logging/LoggerConfig.php deleted file mode 100644 index dd029642..00000000 --- a/src/PHPCensor/Logging/LoggerConfig.php +++ /dev/null @@ -1,71 +0,0 @@ -config = $configArray; - } - - /** - * 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) - { - if (isset($this->cache[$name])) { - return $this->cache[$name]; - } - - $handlers = $this->getHandlers(self::KEY_ALWAYS_LOADED); - if ($name !== self::KEY_ALWAYS_LOADED) { - $handlers = array_merge($handlers, $this->getHandlers($name)); - } - - $logger = new Logger($name, $handlers); - Handler::register($logger); - $this->cache[$name] = $logger; - - return $logger; - } - - /** - * Return an array of enabled log handlers. - * @param $key - * @return array|mixed - */ - protected function getHandlers($key) - { - $handlers = []; - - // 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; - } -} diff --git a/tests/PHPCensor/Logging/LoggerConfigTest.php b/tests/PHPCensor/Logging/LoggerConfigTest.php deleted file mode 100644 index a62d309f..00000000 --- a/tests/PHPCensor/Logging/LoggerConfigTest.php +++ /dev/null @@ -1,86 +0,0 @@ -getFor("something"); - $this->assertInstanceOf('\Psr\Log\LoggerInterface', $logger); - } - - public function testGetFor_ReturnsMonologInstance() - { - $config = new LoggerConfig([]); - $logger = $config->getFor("something"); - $this->assertInstanceOf('\Monolog\Logger', $logger); - } - - public function testGetFor_AttachesAlwaysPresentHandlers() - { - $expectedHandler = new \Monolog\Handler\NullHandler(); - $config = new LoggerConfig([ - LoggerConfig::KEY_ALWAYS_LOADED => function() use ($expectedHandler) { - return [$expectedHandler]; - } - ]); - - /** @var \Monolog\Logger $logger */ - $logger = $config->getFor("something"); - $actualHandler = $logger->popHandler(); - - $this->assertEquals($expectedHandler, $actualHandler); - } - - public function testGetFor_AttachesSpecificHandlers() - { - $expectedHandler = new \Monolog\Handler\NullHandler(); - $config = new LoggerConfig([ - "Specific" => function() use ($expectedHandler) { - return [$expectedHandler]; - } - ]); - - /** @var \Monolog\Logger $logger */ - $logger = $config->getFor("Specific"); - $actualHandler = $logger->popHandler(); - - $this->assertSame($expectedHandler, $actualHandler); - } - - public function testGetFor_IgnoresAlternativeHandlers() - { - $expectedHandler = new \Monolog\Handler\NullHandler(); - $alternativeHandler = new \Monolog\Handler\NullHandler(); - - $config = new LoggerConfig([ - "Specific" => function() use ($expectedHandler) { - return [$expectedHandler]; - }, - "Other" => function() use ($alternativeHandler) { - return [$alternativeHandler]; - } - ]); - - /** @var \Monolog\Logger $logger */ - $logger = $config->getFor("Specific"); - $actualHandler = $logger->popHandler(); - - $this->assertSame($expectedHandler, $actualHandler); - $this->assertNotSame($alternativeHandler, $actualHandler); - } - - public function testGetFor_SameInstance() - { - $config = new LoggerConfig([]); - - $logger1 = $config->getFor("something"); - $logger2 = $config->getFor("something"); - - $this->assertSame($logger1, $logger2); - } -}