]> git.mxchange.org Git - friendica.git/commitdiff
Create StreamLoggerFactory with tests
authorArt4 <art4@wlabs.de>
Mon, 14 Apr 2025 08:00:31 +0000 (08:00 +0000)
committerArt4 <art4@wlabs.de>
Mon, 14 Apr 2025 08:00:31 +0000 (08:00 +0000)
src/Core/Logger/Factory/StreamLoggerFactory.php [new file with mode: 0644]
tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php [new file with mode: 0644]

diff --git a/src/Core/Logger/Factory/StreamLoggerFactory.php b/src/Core/Logger/Factory/StreamLoggerFactory.php
new file mode 100644 (file)
index 0000000..ef8beec
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+namespace Friendica\Core\Logger\Factory;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
+use Friendica\Core\Logger\Capability\LogChannel;
+use Friendica\Core\Logger\Exception\LoggerArgumentException;
+use Friendica\Core\Logger\Exception\LoggerException;
+use Friendica\Core\Logger\Exception\LogLevelException;
+use Friendica\Core\Logger\Type\StreamLogger;
+use Friendica\Core\Logger\Util\FileSystem;
+use Friendica\Core\Logger\Util\FileSystemUtil;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
+/**
+ * The logger factory for the StreamLogger instance
+ *
+ * @see StreamLogger
+ */
+class StreamLoggerFactory implements LoggerFactory
+{
+       private IManageConfigValues $config;
+
+       private IHaveCallIntrospections $introspection;
+
+       private FileSystemUtil $fileSystem;
+
+       public function __construct(
+               IManageConfigValues $config,
+               IHaveCallIntrospections $introspection,
+               FileSystemUtil $fileSystem
+       ) {
+               $this->config        = $config;
+               $this->introspection = $introspection;
+               $this->fileSystem    = $fileSystem;
+       }
+
+       /**
+        * Creates and returns a PSR-3 Logger instance.
+        *
+        * Calling this method multiple times with the same parameters SHOULD return the same object.
+        *
+        * @param \Psr\Log\LogLevel::* $logLevel The log level
+        * @param \Friendica\Core\Logger\Capability\LogChannel::* $logChannel The log channel
+        *
+        * @throws LoggerArgumentException
+        * @throws LogLevelException
+        */
+       public function createLogger(string $logLevel, string $logChannel): LoggerInterface
+       {
+               $logfile = $this->config->get('system', 'logfile');
+
+               if (!file_exists($logfile) || !is_writable($logfile)) {
+                       throw new LoggerArgumentException(sprintf('"%s" is not a valid logfile.', $logfile));
+               }
+
+               if (! array_key_exists($logLevel, StreamLogger::levelToInt)) {
+                       throw new LogLevelException(sprintf('The log level "%s" is not supported by "%s".', $logLevel, StreamLogger::class));
+               }
+
+               return new StreamLogger(
+                       $logChannel,
+                       $this->introspection,
+                       $this->fileSystem->createStream($logfile),
+                       StreamLogger::levelToInt[$logLevel],
+                       getmypid()
+               );
+       }
+}
diff --git a/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php
new file mode 100644 (file)
index 0000000..5f20203
--- /dev/null
@@ -0,0 +1,87 @@
+<?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\Capability\IHaveCallIntrospections;
+use Friendica\Core\Logger\Capability\LogChannel;
+use Friendica\Core\Logger\Exception\LoggerArgumentException;
+use Friendica\Core\Logger\Exception\LogLevelException;
+use Friendica\Core\Logger\Factory\StreamLoggerFactory;
+use Friendica\Core\Logger\Util\FileSystemUtil;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+
+class StreamLoggerFactoryTest extends TestCase
+{
+       public function testCreateLoggerReturnsPsrLogger(): void
+       {
+               $config = $this->createConfiguredMock(
+                       IManageConfigValues::class,
+                       [
+                               'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt',
+                       ]
+               );
+
+               $factory = new StreamLoggerFactory(
+                       $config,
+                       $this->createStub(IHaveCallIntrospections::class),
+                       $this->createStub(FileSystemUtil::class),
+               );
+
+               $this->assertInstanceOf(
+                       LoggerInterface::class,
+                       $factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
+               );
+       }
+
+       public function testCreateLoggerWithInvalidLogfileThrowsException(): void
+       {
+               $config = $this->createConfiguredMock(
+                       IManageConfigValues::class,
+                       [
+                               'get' => dirname(__DIR__, 1) . '/not-existing-logfile.txt',
+                       ]
+               );
+
+               $factory = new StreamLoggerFactory(
+                       $config,
+                       $this->createStub(IHaveCallIntrospections::class),
+                       $this->createStub(FileSystemUtil::class),
+               );
+
+               $this->expectException(LoggerArgumentException::class);
+               $this->expectExceptionMessage('tests/Unit/Core/Logger/not-existing-logfile.txt" is not a valid logfile.');
+
+               $factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT);
+       }
+
+       public function testCreateLoggerWithInvalidLoglevelThrowsException(): void
+       {
+               $config = $this->createConfiguredMock(
+                       IManageConfigValues::class,
+                       [
+                               'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt',
+                       ]
+               );
+
+               $factory = new StreamLoggerFactory(
+                       $config,
+                       $this->createStub(IHaveCallIntrospections::class),
+                       $this->createStub(FileSystemUtil::class),
+               );
+
+               $this->expectException(LogLevelException::class);
+               $this->expectExceptionMessage('The log level "unsupported-loglevel" is not supported by "Friendica\Core\Logger\Type\StreamLogger".');
+
+               $factory->createLogger('unsupported-loglevel', LogChannel::DEFAULT);
+       }
+}