mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-07 15:54:26 +02:00
Create SyslogLoggerFactory
This commit is contained in:
parent
0b41eb1528
commit
0cec451497
3 changed files with 137 additions and 18 deletions
64
src/Core/Logger/Factory/SyslogLoggerFactory.php
Normal file
64
src/Core/Logger/Factory/SyslogLoggerFactory.php
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
<?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\IHaveCallIntrospections;
|
||||||
|
use Friendica\Core\Logger\Exception\LogLevelException;
|
||||||
|
use Friendica\Core\Logger\Type\SyslogLogger;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The logger factory for the SyslogLogger instance
|
||||||
|
*
|
||||||
|
* @see SyslogLogger
|
||||||
|
*/
|
||||||
|
final class SyslogLoggerFactory implements LoggerFactory
|
||||||
|
{
|
||||||
|
private IManageConfigValues $config;
|
||||||
|
|
||||||
|
private IHaveCallIntrospections $introspection;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
IManageConfigValues $config,
|
||||||
|
IHaveCallIntrospections $introspection
|
||||||
|
) {
|
||||||
|
$this->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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,12 +24,10 @@ class StreamLoggerFactoryTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCreateLoggerReturnsPsrLogger(): void
|
public function testCreateLoggerReturnsPsrLogger(): void
|
||||||
{
|
{
|
||||||
$config = $this->createConfiguredMock(
|
$config = $this->createStub(IManageConfigValues::class);
|
||||||
IManageConfigValues::class,
|
$config->method('get')->willReturnMap([
|
||||||
[
|
['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'],
|
||||||
'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt',
|
]);
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$factory = new StreamLoggerFactory(
|
$factory = new StreamLoggerFactory(
|
||||||
$config,
|
$config,
|
||||||
|
@ -45,12 +43,10 @@ class StreamLoggerFactoryTest extends TestCase
|
||||||
|
|
||||||
public function testCreateLoggerWithInvalidLogfileThrowsException(): void
|
public function testCreateLoggerWithInvalidLogfileThrowsException(): void
|
||||||
{
|
{
|
||||||
$config = $this->createConfiguredMock(
|
$config = $this->createStub(IManageConfigValues::class);
|
||||||
IManageConfigValues::class,
|
$config->method('get')->willReturnMap([
|
||||||
[
|
['system', 'logfile', null, dirname(__DIR__, 1) . '/not-existing-logfile.txt'],
|
||||||
'get' => dirname(__DIR__, 1) . '/not-existing-logfile.txt',
|
]);
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$factory = new StreamLoggerFactory(
|
$factory = new StreamLoggerFactory(
|
||||||
$config,
|
$config,
|
||||||
|
@ -66,12 +62,10 @@ class StreamLoggerFactoryTest extends TestCase
|
||||||
|
|
||||||
public function testCreateLoggerWithInvalidLoglevelThrowsException(): void
|
public function testCreateLoggerWithInvalidLoglevelThrowsException(): void
|
||||||
{
|
{
|
||||||
$config = $this->createConfiguredMock(
|
$config = $this->createStub(IManageConfigValues::class);
|
||||||
IManageConfigValues::class,
|
$config->method('get')->willReturnMap([
|
||||||
[
|
['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'],
|
||||||
'get' => dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt',
|
]);
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$factory = new StreamLoggerFactory(
|
$factory = new StreamLoggerFactory(
|
||||||
$config,
|
$config,
|
||||||
|
|
61
tests/Unit/Core/Logger/Factory/SyslogLoggerFactoryTest.php
Normal file
61
tests/Unit/Core/Logger/Factory/SyslogLoggerFactoryTest.php
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
<?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\LogLevelException;
|
||||||
|
use Friendica\Core\Logger\Factory\SyslogLoggerFactory;
|
||||||
|
use Friendica\Core\Logger\Type\SyslogLogger;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Psr\Log\LogLevel;
|
||||||
|
|
||||||
|
class SyslogLoggerFactoryTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCreateLoggerReturnsPsrLogger(): 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->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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue