]> git.mxchange.org Git - friendica.git/commitdiff
LegacyLoggerFactory uses Logger as factory
authorArt4 <art4@wlabs.de>
Sat, 11 Jan 2025 21:05:35 +0000 (21:05 +0000)
committerArt4 <art4@wlabs.de>
Sat, 11 Jan 2025 21:05:35 +0000 (21:05 +0000)
src/App.php
src/Core/Logger/Factory/LegacyLoggerFactory.php
src/Core/Logger/LoggerManager.php
static/dependencies.config.php
tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php
tests/Unit/Core/Logger/LoggerManagerTest.php

index 0f2a6a76f8a7a2750de111f0f686cf1b0c77021c..6573db427ce36993be232d77565ce179cb50d361 100644 (file)
@@ -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
index 42103127e397d24646a003f18b0bde7f64411c24..485f3b09dcea64636bea57eb90e65ffdff03733a 100644 (file)
@@ -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);
        }
 }
index 35d6de1467f282ad533ac82490c73b2e1bf6c25a..05234dea2316ce8969e91a87fa907188e144f893 100644 (file)
@@ -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();
-       }
 }
index d640a7109f433870ed9ac1dce0dfb2971176c495..f1c2f4e52e3d2648bda150a4a5b5bd9f0f576db1 100644 (file)
@@ -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' => [
index d888a665811a5fd41d5fd67fde79d1b68f85ac1a..9ef920c71f27f04de6bd890fbe9ba0981b3daaef 100644 (file)
@@ -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,
index 3fc5ae1abfe63ead41ec676a964a3619f2531a28..81096eee96a87200f9d41775e2f839ac4fd36d3d 100644 (file)
@@ -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();