From cf3d763731932bd26c66673380b347a11b8b8af6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Nov 2019 17:59:04 +0100 Subject: [PATCH] Replaced monolog-cascade by MonologFactory --- config/autoload/logger.global.php | 90 ++++++++++--------- config/autoload/logger.local.php.dist | 35 ++++---- docker/config/shlink_in_docker.local.php | 21 ++--- .../Command/Db/CreateDatabaseCommandTest.php | 4 +- .../Command/Db/MigrateDatabaseCommandTest.php | 4 +- .../Command/Visit/LocateVisitsCommandTest.php | 2 +- .../test/Util/GeolocationDbUpdaterTest.php | 2 +- 7 files changed, 75 insertions(+), 83 deletions(-) diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index e612d2f4..f0d66336 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -4,67 +4,69 @@ declare(strict_types=1); namespace Shlinkio\Shlink; -use Monolog\Handler\RotatingFileHandler; -use Monolog\Handler\StreamHandler; +use Monolog\Formatter; +use Monolog\Handler; use Monolog\Logger; use Monolog\Processor; +use MonologFactory\DiContainerLoggerFactory; use Psr\Log\LoggerInterface; use const PHP_EOL; +$processors = [ + 'exception_with_new_line' => [ + 'name' => Common\Logger\Processor\ExceptionWithNewLineProcessor::class, + ], + 'psr3' => [ + 'name' => Processor\PsrLogMessageProcessor::class, + ], +]; +$formatter = [ + 'name' => Formatter\LineFormatter::class, + 'params' => [ + 'format' => '[%datetime%] %channel%.%level_name% - %message%' . PHP_EOL, + 'include_stacktraces' => true, + ], +]; + return [ 'logger' => [ - 'formatters' => [ - 'dashed' => [ - 'format' => '[%datetime%] %channel%.%level_name% - %message%' . PHP_EOL, - 'include_stacktraces' => true, - ], - ], - - 'handlers' => [ - 'shlink_rotating_handler' => [ - 'class' => RotatingFileHandler::class, - 'level' => Logger::INFO, - 'filename' => 'data/log/shlink_log.log', - 'max_files' => 30, - 'formatter' => 'dashed', - ], - 'access_handler' => [ - 'class' => StreamHandler::class, - 'level' => Logger::INFO, - 'stream' => 'php://stdout', - ], - ], - - 'processors' => [ - 'exception_with_new_line' => [ - 'class' => Common\Logger\Processor\ExceptionWithNewLineProcessor::class, - ], - 'psr3' => [ - 'class' => Processor\PsrLogMessageProcessor::class, - ], - ], - - 'loggers' => [ - 'Shlink' => [ - 'handlers' => [ - // Using a key allows for this to be overwritten - 'shlink_handler' => 'shlink_rotating_handler', + 'Shlink' => [ + 'name' => 'Shlink', + 'handlers' => [ + 'shlink_handler' => [ + 'name' => Handler\RotatingFileHandler::class, + 'params' => [ + 'level' => Logger::INFO, + 'filename' => 'data/log/shlink_log.log', + 'max_files' => 30, + ], + 'formatter' => $formatter, ], - 'processors' => ['exception_with_new_line', 'psr3'], ], - 'Access' => [ - 'handlers' => ['access_handler'], - 'processors' => ['exception_with_new_line', 'psr3'], + 'processors' => $processors, + ], + 'Access' => [ + 'name' => 'Access', + 'handlers' => [ + 'access_handler' => [ + 'name' => Handler\StreamHandler::class, + 'params' => [ + 'level' => Logger::INFO, + 'stream' => 'php://stdout', + ], + 'formatter' => $formatter, + ], ], + 'processors' => $processors, ], ], 'dependencies' => [ 'factories' => [ - 'Logger_Shlink' => Common\Logger\LoggerFactory::class, - 'Logger_Access' => Common\Logger\LoggerFactory::class, + 'Logger_Shlink' => [DiContainerLoggerFactory::class, 'Shlink'], + 'Logger_Access' => [DiContainerLoggerFactory::class, 'Access'], ], 'aliases' => [ 'logger' => 'Logger_Shlink', diff --git a/config/autoload/logger.local.php.dist b/config/autoload/logger.local.php.dist index 403a26fa..4aa46c68 100644 --- a/config/autoload/logger.local.php.dist +++ b/config/autoload/logger.local.php.dist @@ -8,33 +8,28 @@ use Monolog\Logger; $isSwoole = extension_loaded('swoole'); // For swoole, send logs to standard output -$logger = $isSwoole ? [ - 'handlers' => [ - 'shlink_stdout_handler' => [ - 'class' => StreamHandler::class, +$handler = $isSwoole + ? [ + 'name' => StreamHandler::class, + 'params' => [ 'level' => Logger::DEBUG, 'stream' => 'php://stdout', - 'formatter' => 'dashed', ], - ], - - 'loggers' => [ - 'Shlink' => [ - 'handlers' => [ - 'shlink_handler' => 'shlink_stdout_handler', - ], - ], - ], -] : [ - 'handlers' => [ - 'shlink_rotating_handler' => [ + ] + : [ + 'params' => [ 'level' => Logger::DEBUG, ], - ], -]; + ]; return [ - 'logger' => $logger, + 'logger' => [ + 'Shlink' => [ + 'handlers' => [ + 'shlink_handler' => $handler, + ], + ], + ], ]; diff --git a/docker/config/shlink_in_docker.local.php b/docker/config/shlink_in_docker.local.php index 176e45f5..5da6761d 100644 --- a/docker/config/shlink_in_docker.local.php +++ b/docker/config/shlink_in_docker.local.php @@ -130,19 +130,14 @@ return [ 'not_found_redirects' => $helper->getNotFoundRedirectsConfig(), 'logger' => [ - 'handlers' => [ - 'shlink_stdout_handler' => [ - 'class' => StreamHandler::class, - 'level' => Logger::INFO, - 'stream' => 'php://stdout', - 'formatter' => 'dashed', - ], - ], - - 'loggers' => [ - 'Shlink' => [ - 'handlers' => [ - 'shlink_handler' => 'shlink_stdout_handler', + 'Shlink' => [ + 'handlers' => [ + 'shlink_handler' => [ + 'name' => StreamHandler::class, + 'params' => [ + 'level' => Logger::INFO, + 'stream' => 'php://stdout', + ], ], ], ], diff --git a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php index 89322544..7945ea05 100644 --- a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php +++ b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php @@ -15,7 +15,7 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Helper\ProcessHelper; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\Lock\Factory as Locker; +use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\LockInterface; use Symfony\Component\Process\PhpExecutableFinder; @@ -36,7 +36,7 @@ class CreateDatabaseCommandTest extends TestCase public function setUp(): void { - $locker = $this->prophesize(Locker::class); + $locker = $this->prophesize(LockFactory::class); $lock = $this->prophesize(LockInterface::class); $lock->acquire(Argument::any())->willReturn(true); $lock->release()->will(function () { diff --git a/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php b/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php index 1e7690ae..be36a980 100644 --- a/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php +++ b/module/CLI/test/Command/Db/MigrateDatabaseCommandTest.php @@ -12,7 +12,7 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Helper\ProcessHelper; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Component\Lock\Factory as Locker; +use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\LockInterface; use Symfony\Component\Process\PhpExecutableFinder; @@ -25,7 +25,7 @@ class MigrateDatabaseCommandTest extends TestCase public function setUp(): void { - $locker = $this->prophesize(Locker::class); + $locker = $this->prophesize(LockFactory::class); $lock = $this->prophesize(LockInterface::class); $lock->acquire(Argument::any())->willReturn(true); $lock->release()->will(function () { diff --git a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php index bb3be84c..e01bf85f 100644 --- a/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php @@ -48,7 +48,7 @@ class LocateVisitsCommandTest extends TestCase $this->ipResolver = $this->prophesize(IpLocationResolverInterface::class); $this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class); - $this->locker = $this->prophesize(Lock\Factory::class); + $this->locker = $this->prophesize(Lock\LockFactory::class); $this->lock = $this->prophesize(Lock\LockInterface::class); $this->lock->acquire(false)->willReturn(true); $this->lock->release()->will(function () { diff --git a/module/CLI/test/Util/GeolocationDbUpdaterTest.php b/module/CLI/test/Util/GeolocationDbUpdaterTest.php index 56819924..f2b0f98c 100644 --- a/module/CLI/test/Util/GeolocationDbUpdaterTest.php +++ b/module/CLI/test/Util/GeolocationDbUpdaterTest.php @@ -38,7 +38,7 @@ class GeolocationDbUpdaterTest extends TestCase $this->dbUpdater = $this->prophesize(DbUpdaterInterface::class); $this->geoLiteDbReader = $this->prophesize(Reader::class); - $this->locker = $this->prophesize(Lock\Factory::class); + $this->locker = $this->prophesize(Lock\LockFactory::class); $this->lock = $this->prophesize(Lock\LockInterface::class); $this->lock->acquire(true)->willReturn(true); $this->lock->release()->will(function () {