]> git.mxchange.org Git - friendica.git/commitdiff
Create LoggerManager::changeLogChannel()
authorArt4 <art4@wlabs.de>
Sat, 11 Jan 2025 08:05:50 +0000 (08:05 +0000)
committerArt4 <art4@wlabs.de>
Sat, 11 Jan 2025 08:05:50 +0000 (08:05 +0000)
src/App.php
src/Core/Logger/LoggerManager.php
static/dependencies.config.php
tests/Unit/Core/Logger/LoggerManagerTest.php

index 487707f9fdfe85a5a6947e4cf1e6cb0d6c425faa..8af3219a655fd477bf815017b3aae497b79ac33f 100644 (file)
@@ -19,6 +19,7 @@ use Friendica\Capabilities\ICanHandleRequests;
 use Friendica\Content\Nav;
 use Friendica\Core\Config\Factory\Config;
 use Friendica\Core\Container;
+use Friendica\Core\Logger\LoggerManager;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\Database\Definition\DbaDefinition;
@@ -136,6 +137,10 @@ class App
 
                $this->container->setup(LogChannel::APP, false);
 
+               /** @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);
index 4524e80f59c7df29c024aa7f370d7ccaedf5c39e..35d6de1467f282ad533ac82490c73b2e1bf6c25a 100644 (file)
@@ -22,17 +22,23 @@ use Psr\Log\NullLogger;
  */
 final class LoggerManager
 {
+       /**
+        * Workaround: $logger must be static
+        * because Dice always creates a new LoggerManager object
+        *
+        * @var LoggerInterface|null
+        */
+       private static $logger = null;
+
        private IManageConfigValues $config;
 
        private bool $debug;
 
        private string $logLevel;
 
-       private string $logChannel;
-
        private bool $profiling;
 
-       private LoggerInterface $logger;
+       private string $logChannel;
 
        public function __construct(IManageConfigValues $config)
        {
@@ -40,8 +46,15 @@ final class LoggerManager
 
                $this->debug      = (bool) $config->get('system', 'debugging') ?? false;
                $this->logLevel   = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE;
-               $this->logChannel = LogChannel::DEFAULT;
                $this->profiling  = (bool) $config->get('system', 'profiling') ?? false;
+               $this->logChannel = LogChannel::DEFAULT;
+       }
+
+       public function changeLogChannel(string $logChannel): void
+       {
+               $this->logChannel = $logChannel;
+
+               self::$logger = null;
        }
 
        /**
@@ -49,11 +62,11 @@ final class LoggerManager
         */
        public function getLogger(): LoggerInterface
        {
-               if (! isset($this->logger)) {
-                       $this->logger = $this->createProfiledLogger();
+               if (self::$logger === null) {
+                       self::$logger = $this->createProfiledLogger();
                }
 
-               return $this->logger;
+               return self::$logger;
        }
 
        private function createProfiledLogger(): LoggerInterface
index 28ae277c218325895f005d5edbb8ff6fcc5977fd..a2c12426b68a95fc2f0b1d0a0aa754f1d709bb0e 100644 (file)
@@ -162,6 +162,13 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo
                                ['create', [], Dice::CHAIN_CALL],
                        ],
                ],
+               '$LoggerInterface' => [
+                       'shared' => false,
+                       'instanceOf' => \Friendica\Core\Logger\LoggerManager::class,
+                       'call' => [
+                               ['getLogger', [], Dice::CHAIN_CALL],
+                       ],
+               ],
                \Friendica\Core\Logger\Type\SyslogLogger::class => [
                        'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class,
                        'call' => [
index d85c4e0c3a485cdcff5b589c26ef9b4c35ab507d..3fc5ae1abfe63ead41ec676a964a3619f2531a28 100644 (file)
@@ -10,6 +10,7 @@ declare(strict_types=1);
 namespace Friendica\Test\Unit\Core\Logger;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Capability\LogChannel;
 use Friendica\Core\Logger\LoggerManager;
 use Friendica\Core\Logger\Type\ProfilerLogger;
 use PHPUnit\Framework\TestCase;
@@ -20,6 +21,10 @@ class LoggerManagerTest extends TestCase
 {
        public function testGetLoggerReturnsPsrLogger(): void
        {
+               $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
+               $reflectionProperty->setAccessible(true);
+               $reflectionProperty->setValue(null, null);
+
                $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
 
                $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger());
@@ -27,6 +32,10 @@ class LoggerManagerTest extends TestCase
 
        public function testGetLoggerReturnsSameObject(): void
        {
+               $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
+               $reflectionProperty->setAccessible(true);
+               $reflectionProperty->setValue(null, null);
+
                $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
 
                $this->assertSame($factory->getLogger(), $factory->getLogger());
@@ -39,6 +48,10 @@ class LoggerManagerTest extends TestCase
                        ['system', 'debugging', null, false],
                ]);
 
+               $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
+               $reflectionProperty->setAccessible(true);
+               $reflectionProperty->setValue(null, null);
+
                $factory = new LoggerManager($config);
 
                $this->assertInstanceOf(NullLogger::class, $factory->getLogger());
@@ -52,8 +65,33 @@ class LoggerManagerTest extends TestCase
                        ['system', 'profiling', null, true],
                ]);
 
+               $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
+               $reflectionProperty->setAccessible(true);
+               $reflectionProperty->setValue(null, null);
+
                $factory = new LoggerManager($config);
 
                $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger());
        }
+
+       public function testChangeChannelReturnsDifferentLogger(): void
+       {
+               $config = $this->createStub(IManageConfigValues::class);
+               $config->method('get')->willReturnMap([
+                       ['system', 'debugging', null, false],
+                       ['system', 'profiling', null, true],
+               ]);
+
+               $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
+               $reflectionProperty->setAccessible(true);
+               $reflectionProperty->setValue(null, null);
+
+               $factory = new LoggerManager($config);
+
+               $logger1 = $factory->getLogger();
+
+               $factory->changeLogChannel(LogChannel::CONSOLE);
+
+               $this->assertNotSame($logger1, $factory->getLogger());
+       }
 }