]> git.mxchange.org Git - friendica.git/commitdiff
Always return NullLogger if debug is disabled
authorArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 12:36:13 +0000 (12:36 +0000)
committerArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 12:36:13 +0000 (12:36 +0000)
src/Core/Logger/Factory/LoggerFactory.php
tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php

index c789028a08b225d2d3e2c57f3f82c80bdfc235b3..f3c725deff4d3665b93b83f771d6ce7376346ea7 100644 (file)
@@ -9,6 +9,7 @@ declare(strict_types=1);
 
 namespace Friendica\Core\Logger\Factory;
 
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
 
@@ -17,14 +18,31 @@ use Psr\Log\NullLogger;
  */
 final class LoggerFactory
 {
+       private bool $debug;
+
        private LoggerInterface $logger;
 
+       public function __construct(IManageConfigValues $config)
+       {
+               $this->debug = (bool) $config->get('system', 'debugging') ?? false;
+       }
+
        public function create(): LoggerInterface
        {
                if (! isset($this->logger)) {
-                       $this->logger = new NullLogger();
+                       $this->logger = $this->createLogger();
                }
 
                return $this->logger;
        }
+
+       private function createLogger(): LoggerInterface
+       {
+               // Always return NullLogger if debug is disabled
+               if ($this->debug === false) {
+                       return new NullLogger();
+               }
+
+               return new NullLogger();
+       }
 }
index 1c42ec39ddfdcd51c6c9e707462526b5b993c18a..aa80dffe83bdc196c68928f2aae5f830374751ad 100644 (file)
@@ -9,23 +9,35 @@ declare(strict_types=1);
 
 namespace Friendica\Test\Unit\Core\Logger\Factory;
 
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Logger\Factory\LoggerFactory;
 use PHPUnit\Framework\TestCase;
 use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
 
 class LoggerFactoryTest extends TestCase
 {
-       public function testLoggerFactoryCreateReturnsPsrLogger(): void
+       public function testCreateReturnsPsrLogger(): void
        {
-               $factory = new LoggerFactory();
+               $factory = new LoggerFactory($this->createStub(IManageConfigValues::class));
 
                $this->assertInstanceOf(LoggerInterface::class, $factory->create());
        }
 
-       public function testLoggerFactoryCreateReturnsSameObject(): void
+       public function testCreateReturnsSameObject(): void
        {
-               $factory = new LoggerFactory();
+               $factory = new LoggerFactory($this->createStub(IManageConfigValues::class));
 
                $this->assertSame($factory->create(), $factory->create());
        }
+
+       public function testCreateWithDebugDisabledReturnsNullLogger(): void
+       {
+               $config = $this->createStub(IManageConfigValues::class);
+               $config->method('get')->willReturn(false);
+
+               $factory = new LoggerFactory($config);
+
+               $this->assertInstanceOf(NullLogger::class, $factory->create());
+       }
 }