]> git.mxchange.org Git - friendica.git/commitdiff
Rename LoggerFactory into LoggerManager
authorArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 15:21:56 +0000 (15:21 +0000)
committerArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 15:21:56 +0000 (15:21 +0000)
src/Core/Logger/Factory/LoggerFactory.php [deleted file]
src/Core/Logger/LoggerManager.php [new file with mode: 0644]
tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php [deleted file]
tests/Unit/Core/Logger/LoggerManagerTest.php [new file with mode: 0644]

diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php
deleted file mode 100644 (file)
index 5742fdd..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2024, the Friendica project
-// SPDX-FileCopyrightText: 2010-2024 the Friendica project
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-declare(strict_types=1);
-
-namespace Friendica\Core\Logger\Factory;
-
-use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\Core\Logger\Capability\LogChannel;
-use Friendica\Core\Logger\Type\ProfilerLogger;
-use Friendica\Util\Profiler;
-use Psr\Log\LoggerInterface;
-use Psr\Log\LogLevel;
-use Psr\Log\NullLogger;
-
-/**
- * The logger factory for the core logging instances
- */
-final class LoggerFactory
-{
-       private IManageConfigValues $config;
-
-       private bool $debug;
-
-       private string $logLevel;
-
-       private string $logChannel;
-
-       private bool $profiling;
-
-       private LoggerInterface $logger;
-
-       public function __construct(IManageConfigValues $config)
-       {
-               $this->config = $config;
-
-               $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;
-       }
-
-       public function create(): LoggerInterface
-       {
-               if (! isset($this->logger)) {
-                       $this->logger = $this->createProfiledLogger();
-               }
-
-               return $this->logger;
-       }
-
-       private function createProfiledLogger(): LoggerInterface
-       {
-               // Always return NullLogger if debug is disabled
-               if ($this->debug === false) {
-                       $logger = new NullLogger();
-               } else {
-                       $logger = $this->createLogger($this->logLevel, $this->logChannel);
-               }
-
-               if ($this->profiling === true) {
-                       $profiler = new Profiler($this->config);
-
-                       $logger = new ProfilerLogger($logger, $profiler);
-               }
-
-               return $logger;
-       }
-
-       private function createLogger(string $logLevel, string $logChannel): LoggerInterface
-       {
-               return new NullLogger();
-       }
-}
diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php
new file mode 100644 (file)
index 0000000..4524e80
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Core\Logger;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Capability\LogChannel;
+use Friendica\Core\Logger\Type\ProfilerLogger;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+use Psr\Log\NullLogger;
+
+/**
+ * Manager for the core logging instances
+ */
+final class LoggerManager
+{
+       private IManageConfigValues $config;
+
+       private bool $debug;
+
+       private string $logLevel;
+
+       private string $logChannel;
+
+       private bool $profiling;
+
+       private LoggerInterface $logger;
+
+       public function __construct(IManageConfigValues $config)
+       {
+               $this->config = $config;
+
+               $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;
+       }
+
+       /**
+        * (Creates and) Returns the logger instance
+        */
+       public function getLogger(): LoggerInterface
+       {
+               if (! isset($this->logger)) {
+                       $this->logger = $this->createProfiledLogger();
+               }
+
+               return $this->logger;
+       }
+
+       private function createProfiledLogger(): LoggerInterface
+       {
+               // Always return NullLogger if debug is disabled
+               if ($this->debug === false) {
+                       $logger = new NullLogger();
+               } else {
+                       $logger = $this->createLogger($this->logLevel, $this->logChannel);
+               }
+
+               if ($this->profiling === true) {
+                       $profiler = new Profiler($this->config);
+
+                       $logger = new ProfilerLogger($logger, $profiler);
+               }
+
+               return $logger;
+       }
+
+       private function createLogger(string $logLevel, string $logChannel): LoggerInterface
+       {
+               return new NullLogger();
+       }
+}
diff --git a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php
deleted file mode 100644 (file)
index e97f79f..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2024, the Friendica project
-// SPDX-FileCopyrightText: 2010-2024 the Friendica project
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-declare(strict_types=1);
-
-namespace Friendica\Test\Unit\Core\Logger\Factory;
-
-use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\Core\Logger\Factory\LoggerFactory;
-use Friendica\Core\Logger\Type\ProfilerLogger;
-use PHPUnit\Framework\TestCase;
-use Psr\Log\LoggerInterface;
-use Psr\Log\NullLogger;
-
-class LoggerFactoryTest extends TestCase
-{
-       public function testCreateReturnsPsrLogger(): void
-       {
-               $factory = new LoggerFactory($this->createStub(IManageConfigValues::class));
-
-               $this->assertInstanceOf(LoggerInterface::class, $factory->create());
-       }
-
-       public function testCreateReturnsSameObject(): void
-       {
-               $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')->willReturnMap([
-                       ['system', 'debugging', null, false],
-               ]);
-
-               $factory = new LoggerFactory($config);
-
-               $this->assertInstanceOf(NullLogger::class, $factory->create());
-       }
-
-       public function testCreateWithProfilerEnabledReturnsProfilerLogger(): void
-       {
-               $config = $this->createStub(IManageConfigValues::class);
-               $config->method('get')->willReturnMap([
-                       ['system', 'debugging', null, false],
-                       ['system', 'profiling', null, true],
-               ]);
-
-               $factory = new LoggerFactory($config);
-
-               $this->assertInstanceOf(ProfilerLogger::class, $factory->create());
-       }
-}
diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php
new file mode 100644 (file)
index 0000000..d85c4e0
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Test\Unit\Core\Logger;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\LoggerManager;
+use Friendica\Core\Logger\Type\ProfilerLogger;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
+class LoggerManagerTest extends TestCase
+{
+       public function testGetLoggerReturnsPsrLogger(): void
+       {
+               $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
+
+               $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger());
+       }
+
+       public function testGetLoggerReturnsSameObject(): void
+       {
+               $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
+
+               $this->assertSame($factory->getLogger(), $factory->getLogger());
+       }
+
+       public function testGetLoggerWithDebugDisabledReturnsNullLogger(): void
+       {
+               $config = $this->createStub(IManageConfigValues::class);
+               $config->method('get')->willReturnMap([
+                       ['system', 'debugging', null, false],
+               ]);
+
+               $factory = new LoggerManager($config);
+
+               $this->assertInstanceOf(NullLogger::class, $factory->getLogger());
+       }
+
+       public function testGetLoggerWithProfilerEnabledReturnsProfilerLogger(): void
+       {
+               $config = $this->createStub(IManageConfigValues::class);
+               $config->method('get')->willReturnMap([
+                       ['system', 'debugging', null, false],
+                       ['system', 'profiling', null, true],
+               ]);
+
+               $factory = new LoggerManager($config);
+
+               $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger());
+       }
+}