From 8dbbf882a8b21c8e7c7398db7e0cad8d81f2a6b8 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 17 Jul 2023 01:16:29 +0200 Subject: [PATCH] Fix SyslogLogger and tests --- src/Core/Logger/Factory/SyslogLogger.php | 6 +-- tests/functional/DependencyCheckTest.php | 2 +- .../Logger/SyslogLoggerFactoryWrapper.php | 47 +++++++++++++++++++ tests/src/Core/Logger/SyslogLoggerTest.php | 15 +++--- tests/src/Core/Logger/SyslogLoggerWrapper.php | 8 ++-- 5 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 tests/src/Core/Logger/SyslogLoggerFactoryWrapper.php diff --git a/src/Core/Logger/Factory/SyslogLogger.php b/src/Core/Logger/Factory/SyslogLogger.php index 385625862..7b1712344 100644 --- a/src/Core/Logger/Factory/SyslogLogger.php +++ b/src/Core/Logger/Factory/SyslogLogger.php @@ -23,9 +23,9 @@ namespace Friendica\Core\Logger\Factory; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Exception\LoggerException; +use Friendica\Core\Logger\Exception\LogLevelException; use Friendica\Core\Logger\Type\SyslogLogger as SyslogLoggerClass; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; /** * The logger factory for the SyslogLogger instance @@ -49,10 +49,10 @@ class SyslogLogger extends AbstractLoggerTypeFactory $logFacility = $config->get('system', 'syslog_facility') ?? SyslogLoggerClass::DEFAULT_FACILITY; $loglevel = SyslogLogger::mapLegacyConfigDebugLevel($config->get('system', 'loglevel')); - if (!array_key_exists($loglevel, SyslogLoggerClass::logLevels)) { + if (array_key_exists($loglevel, SyslogLoggerClass::logLevels)) { $loglevel = SyslogLoggerClass::logLevels[$loglevel]; } else { - $loglevel = SyslogLoggerClass::logLevels[LogLevel::NOTICE]; + throw new LogLevelException(sprintf('The level "%s" is not valid.', $loglevel)); } return new SyslogLoggerClass($this->channel, $this->introspection, $loglevel, $logOpts, $logFacility); diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 89b8f79a5..4488fe586 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -145,7 +145,7 @@ class DependencyCheckTest extends FixtureTest $config->set('system', 'dlogfile', $this->root->url() . '/friendica.log'); /** @var LoggerInterface $logger */ - $logger = $this->dice->create('$devLogger', [['$channel' => 'dev']]); + $logger = $this->dice->create('$devLogger', ['dev']); self::assertInstanceOf(LoggerInterface::class, $logger); } diff --git a/tests/src/Core/Logger/SyslogLoggerFactoryWrapper.php b/tests/src/Core/Logger/SyslogLoggerFactoryWrapper.php new file mode 100644 index 000000000..30aaa4477 --- /dev/null +++ b/tests/src/Core/Logger/SyslogLoggerFactoryWrapper.php @@ -0,0 +1,47 @@ +. + * + */ + +namespace Friendica\Test\src\Core\Logger; + +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Logger\Exception\LogLevelException; +use Friendica\Core\Logger\Factory\SyslogLogger; +use Friendica\Core\Logger\Type\SyslogLogger as SyslogLoggerClass; +use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; + +class SyslogLoggerFactoryWrapper extends SyslogLogger +{ + public function create(IManageConfigValues $config): LoggerInterface + { + $logOpts = $config->get('system', 'syslog_flags') ?? SyslogLoggerClass::DEFAULT_FLAGS; + $logFacility = $config->get('system', 'syslog_facility') ?? SyslogLoggerClass::DEFAULT_FACILITY; + $loglevel = SyslogLogger::mapLegacyConfigDebugLevel($config->get('system', 'loglevel')); + + if (array_key_exists($loglevel, SyslogLoggerClass::logLevels)) { + $loglevel = SyslogLoggerClass::logLevels[$loglevel]; + } else { + throw new LogLevelException(sprintf('The level "%s" is not valid.', $loglevel)); + } + + return new SyslogLoggerWrapper($this->channel, $this->introspection, $loglevel, $logOpts, $logFacility); + } +} diff --git a/tests/src/Core/Logger/SyslogLoggerTest.php b/tests/src/Core/Logger/SyslogLoggerTest.php index 53ace79c0..c22aecc10 100644 --- a/tests/src/Core/Logger/SyslogLoggerTest.php +++ b/tests/src/Core/Logger/SyslogLoggerTest.php @@ -21,8 +21,6 @@ namespace Friendica\Test\src\Core\Logger; -use Friendica\Core\Logger\Exception\LoggerArgumentException; -use Friendica\Core\Logger\Exception\LoggerException; use Friendica\Core\Logger\Exception\LogLevelException; use Friendica\Core\Logger\Type\SyslogLogger; use Psr\Log\LogLevel; @@ -58,7 +56,10 @@ class SyslogLoggerTest extends AbstractLoggerTest */ protected function getInstance($level = LogLevel::DEBUG) { - $this->logger = new SyslogLoggerWrapper('test', $this->config, $this->introspection, $level); + $this->config->shouldReceive('get')->with('system', 'loglevel')->andReturn($level); + + $loggerFactory = new SyslogLoggerFactoryWrapper($this->introspection, 'test'); + $this->logger = $loggerFactory->create($this->config); return $this->logger; } @@ -71,8 +72,8 @@ class SyslogLoggerTest extends AbstractLoggerTest { $this->expectException(LogLevelException::class); $this->expectExceptionMessageMatches("/The level \".*\" is not valid./"); - - $logger = new SyslogLoggerWrapper('test', $this->config, $this->introspection, 'NOPE'); + + $logger = $this->getInstance('NOPE'); } /** @@ -83,7 +84,7 @@ class SyslogLoggerTest extends AbstractLoggerTest $this->expectException(LogLevelException::class); $this->expectExceptionMessageMatches("/The level \".*\" is not valid./"); - $logger = new SyslogLoggerWrapper('test', $this->config, $this->introspection); + $logger = $this->getInstance(); $logger->log('NOPE', 'a test'); } @@ -94,7 +95,7 @@ class SyslogLoggerTest extends AbstractLoggerTest */ public function testClose() { - $logger = new SyslogLoggerWrapper('test', $this->config, $this->introspection); + $logger = $this->getInstance(); $logger->emergency('test'); $logger->close(); // Reopened itself diff --git a/tests/src/Core/Logger/SyslogLoggerWrapper.php b/tests/src/Core/Logger/SyslogLoggerWrapper.php index 9fd16706a..1e0c4535f 100644 --- a/tests/src/Core/Logger/SyslogLoggerWrapper.php +++ b/tests/src/Core/Logger/SyslogLoggerWrapper.php @@ -21,10 +21,8 @@ namespace Friendica\Test\src\Core\Logger; -use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections; use Friendica\Core\Logger\Type\SyslogLogger; -use Friendica\Core\Logger\Util\Introspection; -use Psr\Log\LogLevel; /** * Wraps the SyslogLogger for replacing the syslog call with a string field. @@ -33,9 +31,9 @@ class SyslogLoggerWrapper extends SyslogLogger { private $content; - public function __construct($channel, IManageConfigValues $config, Introspection $introspection, $level = LogLevel::NOTICE) + public function __construct(string $channel, IHaveCallIntrospections $introspection, string $logLevel, string $logOptions, string $logFacility) { - parent::__construct($channel, $config, $introspection, $level); + parent::__construct($channel, $introspection, $logLevel, $logOptions, $logFacility); $this->content = ''; }