Added daily rotate logger for console commands. Issue #108.
This commit is contained in:
parent
c78521e7bf
commit
1bc8dfd5eb
|
@ -36,6 +36,9 @@ php-censor:
|
||||||
host: localhost
|
host: localhost
|
||||||
name: php-censor-queue
|
name: php-censor-queue
|
||||||
lifetime: 600
|
lifetime: 600
|
||||||
|
log:
|
||||||
|
rotate: true
|
||||||
|
max_files: 10
|
||||||
bitbucket:
|
bitbucket:
|
||||||
username: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
username: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
app_password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
app_password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
||||||
|
|
|
@ -253,6 +253,10 @@ class InstallCommand extends Command
|
||||||
'per_page' => 10,
|
'per_page' => 10,
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'queue' => $queueConfig,
|
'queue' => $queueConfig,
|
||||||
|
'log' => [
|
||||||
|
'rotate' => false,
|
||||||
|
'max_files' => 0,
|
||||||
|
],
|
||||||
'email_settings' => [
|
'email_settings' => [
|
||||||
'from_address' => 'PHP Censor <no-reply@php-censor.local>',
|
'from_address' => 'PHP Censor <no-reply@php-censor.local>',
|
||||||
'smtp_address' => null,
|
'smtp_address' => null,
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace PHPCensor\Console;
|
||||||
|
|
||||||
use b8\Config;
|
use b8\Config;
|
||||||
use b8\Store\Factory;
|
use b8\Store\Factory;
|
||||||
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use PHPCensor\Command\CreateAdminCommand;
|
use PHPCensor\Command\CreateAdminCommand;
|
||||||
|
@ -13,7 +14,7 @@ use PHPCensor\Command\RebuildCommand;
|
||||||
use PHPCensor\Command\RebuildQueueCommand;
|
use PHPCensor\Command\RebuildQueueCommand;
|
||||||
use PHPCensor\Command\RunCommand;
|
use PHPCensor\Command\RunCommand;
|
||||||
use PHPCensor\Command\WorkerCommand;
|
use PHPCensor\Command\WorkerCommand;
|
||||||
use PHPCensor\Logging\LoggerConfig;
|
use PHPCensor\Logging\Handler;
|
||||||
use PHPCensor\Service\BuildService;
|
use PHPCensor\Service\BuildService;
|
||||||
use PHPCensor\Store\BuildStore;
|
use PHPCensor\Store\BuildStore;
|
||||||
use PHPCensor\Store\ProjectStore;
|
use PHPCensor\Store\ProjectStore;
|
||||||
|
@ -32,6 +33,29 @@ use Phinx\Config\Config as PhinxConfig;
|
||||||
*/
|
*/
|
||||||
class Application extends BaseApplication
|
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.
|
* Constructor.
|
||||||
*
|
*
|
||||||
|
@ -42,14 +66,6 @@ class Application extends BaseApplication
|
||||||
{
|
{
|
||||||
parent::__construct($name, $version);
|
parent::__construct($name, $version);
|
||||||
|
|
||||||
$loggerConfig = new LoggerConfig([
|
|
||||||
"_" => function() {
|
|
||||||
return [
|
|
||||||
new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
$applicationConfig = Config::getInstance();
|
$applicationConfig = Config::getInstance();
|
||||||
$databaseSettings = $applicationConfig->get('b8.database', []);
|
$databaseSettings = $applicationConfig->get('b8.database', []);
|
||||||
|
|
||||||
|
@ -109,12 +125,14 @@ class Application extends BaseApplication
|
||||||
/** @var BuildStore $buildStore */
|
/** @var BuildStore $buildStore */
|
||||||
$buildStore = Factory::getStore('Build');
|
$buildStore = Factory::getStore('Build');
|
||||||
|
|
||||||
$this->add(new RunCommand($loggerConfig->getFor('RunCommand')));
|
$logger = $this->initLogger($applicationConfig);
|
||||||
$this->add(new RebuildCommand($loggerConfig->getFor('RunCommand')));
|
|
||||||
|
$this->add(new RunCommand($logger));
|
||||||
|
$this->add(new RebuildCommand($logger));
|
||||||
$this->add(new InstallCommand());
|
$this->add(new InstallCommand());
|
||||||
$this->add(new CreateAdminCommand($userStore));
|
$this->add(new CreateAdminCommand($userStore));
|
||||||
$this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore)));
|
$this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore)));
|
||||||
$this->add(new WorkerCommand($loggerConfig->getFor('WorkerCommand')));
|
$this->add(new WorkerCommand($logger));
|
||||||
$this->add(new RebuildQueueCommand($loggerConfig->getFor('RebuildQueueCommand')));
|
$this->add(new RebuildQueueCommand($logger));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,71 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace PHPCensor\Logging;
|
|
||||||
|
|
||||||
use Monolog\Logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class LoggerConfig
|
|
||||||
*/
|
|
||||||
class LoggerConfig
|
|
||||||
{
|
|
||||||
const KEY_ALWAYS_LOADED = "_";
|
|
||||||
private $config;
|
|
||||||
private $cache = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Each key of the array is the name of a logger. The value of
|
|
||||||
* each key should be an array or a function that returns an
|
|
||||||
* array of LogHandlers.
|
|
||||||
* @param array $configArray
|
|
||||||
*/
|
|
||||||
public function __construct(array $configArray = [])
|
|
||||||
{
|
|
||||||
$this->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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,86 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Tests\PHPCensor\Plugin\Helper;
|
|
||||||
|
|
||||||
use \PHPCensor\Logging\LoggerConfig;
|
|
||||||
|
|
||||||
class LoggerConfigTest extends \PHPUnit\Framework\TestCase
|
|
||||||
{
|
|
||||||
public function testGetFor_ReturnsPSRLogger()
|
|
||||||
{
|
|
||||||
$config = new LoggerConfig([]);
|
|
||||||
$logger = $config->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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue