diff --git a/src/App.php b/src/App.php index 0f2a6a76f8..6573db427c 100644 --- a/src/App.php +++ b/src/App.php @@ -146,10 +146,6 @@ class App $this->registerErrorHandler(); - /** @var LoggerManager */ - $loggerManager = $this->container->create(LoggerManager::class); - $loggerManager->changeLogChannel(LogChannel::APP); - $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); $this->config = $this->container->create(IManageConfigValues::class); @@ -251,9 +247,9 @@ class App private function setupContainerForLogger(string $logChannel): void { - $this->container->addRule(LoggerInterface::class, [ - 'constructParams' => [$logChannel], - ]); + /** @var LoggerManager */ + $loggerManager = $this->container->create(LoggerManager::class); + $loggerManager->changeLogChannel($logChannel); } private function setupLegacyServiceLocator(): void diff --git a/src/Core/Logger/Factory/LegacyLoggerFactory.php b/src/Core/Logger/Factory/LegacyLoggerFactory.php index 42103127e3..485f3b09dc 100644 --- a/src/Core/Logger/Factory/LegacyLoggerFactory.php +++ b/src/Core/Logger/Factory/LegacyLoggerFactory.php @@ -9,15 +9,29 @@ declare(strict_types=1); namespace Friendica\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Hooks\Capability\ICanCreateInstances; +use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; -use Psr\Log\NullLogger; /** * Manager for the core logging instances */ final class LegacyLoggerFactory implements LoggerFactory { + private ICanCreateInstances $instanceCreator; + + private IManageConfigValues $config; + + private Profiler $profiler; + + public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler) + { + $this->instanceCreator = $instanceCreator; + $this->config = $config; + $this->profiler = $profiler; + } + /** * Creates and returns a PSR-3 Logger instance. * @@ -28,6 +42,8 @@ final class LegacyLoggerFactory implements LoggerFactory */ public function createLogger(string $logLevel, string $logChannel): LoggerInterface { - return new NullLogger(); + $factory = new Logger($logChannel); + + return $factory->create($this->instanceCreator, $this->config, $this->profiler); } } diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index 35d6de1467..05234dea23 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -11,6 +11,7 @@ namespace Friendica\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\Type\ProfilerLogger; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -32,6 +33,8 @@ final class LoggerManager private IManageConfigValues $config; + private LoggerFactory $factory; + private bool $debug; private string $logLevel; @@ -40,9 +43,10 @@ final class LoggerManager private string $logChannel; - public function __construct(IManageConfigValues $config) + public function __construct(IManageConfigValues $config, LoggerFactory $factory) { - $this->config = $config; + $this->config = $config; + $this->factory = $factory; $this->debug = (bool) $config->get('system', 'debugging') ?? false; $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; @@ -71,11 +75,11 @@ final class LoggerManager private function createProfiledLogger(): LoggerInterface { - // Always return NullLogger if debug is disabled + // Always create NullLogger if debug is disabled if ($this->debug === false) { $logger = new NullLogger(); } else { - $logger = $this->createLogger($this->logLevel, $this->logChannel); + $logger = $this->factory->createLogger($this->logLevel, $this->logChannel); } if ($this->profiling === true) { @@ -86,9 +90,4 @@ final class LoggerManager return $logger; } - - private function createLogger(string $logLevel, string $logChannel): LoggerInterface - { - return new NullLogger(); - } } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index d640a7109f..f1c2f4e52e 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -157,18 +157,19 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo ], ], \Psr\Log\LoggerInterface::class => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], - ], - ], - '$LoggerInterface' => [ - 'shared' => false, 'instanceOf' => \Friendica\Core\Logger\LoggerManager::class, 'call' => [ ['getLogger', [], Dice::CHAIN_CALL], ], ], + \Friendica\Core\Logger\LoggerManager::class => [ + 'substitutions' => [ + \Friendica\Core\Logger\Factory\LoggerFactory::class => \Friendica\Core\Logger\Factory\LegacyLoggerFactory::class, + ], + ], + \Friendica\Core\Logger\Factory\LoggerFactory::class => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\LegacyLoggerFactory::class, + ], \Friendica\Core\Logger\Type\SyslogLogger::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, 'call' => [ diff --git a/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php index d888a66581..9ef920c71f 100644 --- a/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php @@ -9,8 +9,11 @@ declare(strict_types=1); namespace Friendica\Test\Unit\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Hooks\Capability\ICanCreateInstances; use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\Factory\LegacyLoggerFactory; +use Friendica\Util\Profiler; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -19,7 +22,11 @@ class LegacyLoggerFactoryTest extends TestCase { public function testCreateLoggerReturnsPsrLogger(): void { - $factory = new LegacyLoggerFactory(); + $factory = new LegacyLoggerFactory( + $this->createStub(ICanCreateInstances::class), + $this->createStub(IManageConfigValues::class), + $this->createStub(Profiler::class), + ); $this->assertInstanceOf( LoggerInterface::class, diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index 3fc5ae1abf..81096eee96 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -11,6 +11,7 @@ namespace Friendica\Test\Unit\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\Type\ProfilerLogger; use PHPUnit\Framework\TestCase; @@ -25,7 +26,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); + $factory = new LoggerManager( + $this->createStub(IManageConfigValues::class), + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); } @@ -36,7 +40,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); + $factory = new LoggerManager( + $this->createStub(IManageConfigValues::class), + $this->createStub(LoggerFactory::class) + ); $this->assertSame($factory->getLogger(), $factory->getLogger()); } @@ -52,7 +59,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(NullLogger::class, $factory->getLogger()); } @@ -69,7 +79,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); } @@ -86,7 +99,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $logger1 = $factory->getLogger();