diff --git a/src/Core/Logger/Factory/SyslogLoggerFactory.php b/src/Core/Logger/Factory/SyslogLoggerFactory.php new file mode 100644 index 0000000000..44f9d79c73 --- /dev/null +++ b/src/Core/Logger/Factory/SyslogLoggerFactory.php @@ -0,0 +1,64 @@ +config = $config; + $this->introspection = $introspection; + } + + /** + * 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 LogLevelException + */ + public function createLogger(string $logLevel, string $logChannel): LoggerInterface + { + $logOpts = (string) $this->config->get('system', 'syslog_flags') ?? SyslogLogger::DEFAULT_FLAGS; + $logFacility = (string) $this->config->get('system', 'syslog_facility') ?? SyslogLogger::DEFAULT_FACILITY; + + if (!array_key_exists($logLevel, SyslogLogger::logLevels)) { + throw new LogLevelException(sprintf('The log level "%s" is not supported by "%s".', $logLevel, SyslogLogger::class)); + } + + return new SyslogLogger( + $logChannel, + $this->introspection, + (string) SyslogLogger::logLevels[$logLevel], + $logOpts, + $logFacility + ); + } +} diff --git a/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php index 5f2020324f..744d597f19 100644 --- a/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/StreamLoggerFactoryTest.php @@ -24,12 +24,10 @@ class StreamLoggerFactoryTest extends TestCase { public function testCreateLoggerReturnsPsrLogger(): void { - $config = $this->createConfiguredMock( - IManageConfigValues::class, - [ - 'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt', - ] - ); + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'], + ]); $factory = new StreamLoggerFactory( $config, @@ -45,12 +43,10 @@ class StreamLoggerFactoryTest extends TestCase public function testCreateLoggerWithInvalidLogfileThrowsException(): void { - $config = $this->createConfiguredMock( - IManageConfigValues::class, - [ - 'get' => dirname(__DIR__, 1) . '/not-existing-logfile.txt', - ] - ); + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'logfile', null, dirname(__DIR__, 1) . '/not-existing-logfile.txt'], + ]); $factory = new StreamLoggerFactory( $config, @@ -66,12 +62,10 @@ class StreamLoggerFactoryTest extends TestCase public function testCreateLoggerWithInvalidLoglevelThrowsException(): void { - $config = $this->createConfiguredMock( - IManageConfigValues::class, - [ - 'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt', - ] - ); + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'], + ]); $factory = new StreamLoggerFactory( $config, diff --git a/tests/Unit/Core/Logger/Factory/SyslogLoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/SyslogLoggerFactoryTest.php new file mode 100644 index 0000000000..7f94c66fcd --- /dev/null +++ b/tests/Unit/Core/Logger/Factory/SyslogLoggerFactoryTest.php @@ -0,0 +1,61 @@ +createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'syslog_flags', null, SyslogLogger::DEFAULT_FLAGS], + ['system', 'syslog_facility', null, SyslogLogger::DEFAULT_FACILITY], + ]); + + $factory = new SyslogLoggerFactory( + $config, + $this->createStub(IHaveCallIntrospections::class), + ); + + $this->assertInstanceOf( + LoggerInterface::class, + $factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT) + ); + } + + public function testCreateLoggerWithInvalidLoglevelThrowsException(): void + { + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'syslog_flags', null, SyslogLogger::DEFAULT_FLAGS], + ['system', 'syslog_facility', null, SyslogLogger::DEFAULT_FACILITY], + ]); + + $factory = new SyslogLoggerFactory( + $config, + $this->createStub(IHaveCallIntrospections::class), + ); + + $this->expectException(LogLevelException::class); + $this->expectExceptionMessage('The log level "unsupported-loglevel" is not supported by "Friendica\Core\Logger\Type\SyslogLogger".'); + + $factory->createLogger('unsupported-loglevel', LogChannel::DEFAULT); + } +}