From 9bcb470caa53e151f46b9a5e39e2efa504940e64 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 10:30:11 +0000 Subject: [PATCH 01/18] Create new LoggerFactory --- src/Core/Logger/Factory/LoggerFactory.php | 24 +++++++++++++++++++ .../Core/Logger/Factory/LoggerFactoryTest.php | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/Core/Logger/Factory/LoggerFactory.php create mode 100644 tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php new file mode 100644 index 0000000000..77c06d4f89 --- /dev/null +++ b/src/Core/Logger/Factory/LoggerFactory.php @@ -0,0 +1,24 @@ +assertInstanceOf(LoggerInterface::class, $factory->create()); + } +} From 4a8533aa11a86735e5d4061c8f9ea2209d737a5f Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 10:33:55 +0000 Subject: [PATCH 02/18] LoggerFactory returns same object --- src/Core/Logger/Factory/LoggerFactory.php | 8 +++++++- tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php index 77c06d4f89..c789028a08 100644 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ b/src/Core/Logger/Factory/LoggerFactory.php @@ -17,8 +17,14 @@ use Psr\Log\NullLogger; */ final class LoggerFactory { + private LoggerInterface $logger; + public function create(): LoggerInterface { - return new NullLogger(); + if (! isset($this->logger)) { + $this->logger = new NullLogger(); + } + + return $this->logger; } } diff --git a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php index 71ce0c741d..1c42ec39dd 100644 --- a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php @@ -21,4 +21,11 @@ class LoggerFactoryTest extends TestCase $this->assertInstanceOf(LoggerInterface::class, $factory->create()); } + + public function testLoggerFactoryCreateReturnsSameObject(): void + { + $factory = new LoggerFactory(); + + $this->assertSame($factory->create(), $factory->create()); + } } From f65a8385948ae9284e18bdbe1714da3c1e4757f8 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 12:36:13 +0000 Subject: [PATCH 03/18] Always return NullLogger if debug is disabled --- src/Core/Logger/Factory/LoggerFactory.php | 20 ++++++++++++++++++- .../Core/Logger/Factory/LoggerFactoryTest.php | 20 +++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php index c789028a08..f3c725deff 100644 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ b/src/Core/Logger/Factory/LoggerFactory.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace Friendica\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -17,14 +18,31 @@ use Psr\Log\NullLogger; */ final class LoggerFactory { + private bool $debug; + private LoggerInterface $logger; + public function __construct(IManageConfigValues $config) + { + $this->debug = (bool) $config->get('system', 'debugging') ?? false; + } + public function create(): LoggerInterface { if (! isset($this->logger)) { - $this->logger = new NullLogger(); + $this->logger = $this->createLogger(); } return $this->logger; } + + private function createLogger(): LoggerInterface + { + // Always return NullLogger if debug is disabled + if ($this->debug === false) { + return new NullLogger(); + } + + return new NullLogger(); + } } diff --git a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php index 1c42ec39dd..aa80dffe83 100644 --- a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php @@ -9,23 +9,35 @@ declare(strict_types=1); namespace Friendica\Test\Unit\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Factory\LoggerFactory; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; class LoggerFactoryTest extends TestCase { - public function testLoggerFactoryCreateReturnsPsrLogger(): void + public function testCreateReturnsPsrLogger(): void { - $factory = new LoggerFactory(); + $factory = new LoggerFactory($this->createStub(IManageConfigValues::class)); $this->assertInstanceOf(LoggerInterface::class, $factory->create()); } - public function testLoggerFactoryCreateReturnsSameObject(): void + public function testCreateReturnsSameObject(): void { - $factory = new LoggerFactory(); + $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')->willReturn(false); + + $factory = new LoggerFactory($config); + + $this->assertInstanceOf(NullLogger::class, $factory->create()); + } } From 96af0e6ebb66f42585fb9015dcf54ec2040c4602 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 13:28:42 +0000 Subject: [PATCH 04/18] LoggerFactory can create ProfilerLogger --- src/Core/Logger/Factory/LoggerFactory.php | 26 +++++++++++++++++-- .../Core/Logger/Factory/LoggerFactoryTest.php | 18 ++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php index f3c725deff..37830c32e1 100644 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ b/src/Core/Logger/Factory/LoggerFactory.php @@ -10,6 +10,8 @@ declare(strict_types=1); namespace Friendica\Core\Logger\Factory; use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Logger\Type\ProfilerLogger; +use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -18,24 +20,44 @@ use Psr\Log\NullLogger; */ final class LoggerFactory { + private IManageConfigValues $config; + private bool $debug; + private bool $profiling; + private LoggerInterface $logger; public function __construct(IManageConfigValues $config) { - $this->debug = (bool) $config->get('system', 'debugging') ?? false; + $this->config = $config; + + $this->debug = (bool) $config->get('system', 'debugging') ?? false; + $this->profiling = (bool) $config->get('system', 'profiling') ?? false; } public function create(): LoggerInterface { if (! isset($this->logger)) { - $this->logger = $this->createLogger(); + $this->logger = $this->createProfiledLogger(); } return $this->logger; } + private function createProfiledLogger(): LoggerInterface + { + $logger = $this->createLogger(); + + if ($this->profiling === true) { + $profiler = new Profiler($this->config); + + $logger = new ProfilerLogger($logger, $profiler); + } + + return $logger; + } + private function createLogger(): LoggerInterface { // Always return NullLogger if debug is disabled diff --git a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php index aa80dffe83..e97f79f82f 100644 --- a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php @@ -11,6 +11,7 @@ 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; @@ -34,10 +35,25 @@ class LoggerFactoryTest extends TestCase public function testCreateWithDebugDisabledReturnsNullLogger(): void { $config = $this->createStub(IManageConfigValues::class); - $config->method('get')->willReturn(false); + $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()); + } } From c339dc1a079ba249ebdb2983bf7b3cad4de06b56 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 15:15:58 +0000 Subject: [PATCH 05/18] Add support for LogLevel and LogChannel --- src/Core/Logger/Factory/LoggerFactory.php | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php index 37830c32e1..5742fdd5b5 100644 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ b/src/Core/Logger/Factory/LoggerFactory.php @@ -10,9 +10,11 @@ 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; /** @@ -24,6 +26,10 @@ final class LoggerFactory private bool $debug; + private string $logLevel; + + private string $logChannel; + private bool $profiling; private LoggerInterface $logger; @@ -32,8 +38,10 @@ final class LoggerFactory { $this->config = $config; - $this->debug = (bool) $config->get('system', 'debugging') ?? false; - $this->profiling = (bool) $config->get('system', 'profiling') ?? false; + $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 @@ -47,7 +55,12 @@ final class LoggerFactory private function createProfiledLogger(): LoggerInterface { - $logger = $this->createLogger(); + // 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); @@ -58,13 +71,8 @@ final class LoggerFactory return $logger; } - private function createLogger(): LoggerInterface + private function createLogger(string $logLevel, string $logChannel): LoggerInterface { - // Always return NullLogger if debug is disabled - if ($this->debug === false) { - return new NullLogger(); - } - return new NullLogger(); } } From 2df76617c17d3a090f582e5bcb214312f7f601bf Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 15:21:56 +0000 Subject: [PATCH 06/18] Rename LoggerFactory into LoggerManager --- .../LoggerFactory.php => LoggerManager.php} | 11 ++-- .../Core/Logger/Factory/LoggerFactoryTest.php | 59 ------------------- tests/Unit/Core/Logger/LoggerManagerTest.php | 59 +++++++++++++++++++ 3 files changed, 66 insertions(+), 63 deletions(-) rename src/Core/Logger/{Factory/LoggerFactory.php => LoggerManager.php} (89%) delete mode 100644 tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php create mode 100644 tests/Unit/Core/Logger/LoggerManagerTest.php diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/LoggerManager.php similarity index 89% rename from src/Core/Logger/Factory/LoggerFactory.php rename to src/Core/Logger/LoggerManager.php index 5742fdd5b5..4524e80f59 100644 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ b/src/Core/Logger/LoggerManager.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace Friendica\Core\Logger\Factory; +namespace Friendica\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; @@ -18,9 +18,9 @@ use Psr\Log\LogLevel; use Psr\Log\NullLogger; /** - * The logger factory for the core logging instances + * Manager for the core logging instances */ -final class LoggerFactory +final class LoggerManager { private IManageConfigValues $config; @@ -44,7 +44,10 @@ final class LoggerFactory $this->profiling = (bool) $config->get('system', 'profiling') ?? false; } - public function create(): LoggerInterface + /** + * (Creates and) Returns the logger instance + */ + public function getLogger(): LoggerInterface { if (! isset($this->logger)) { $this->logger = $this->createProfiledLogger(); diff --git a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php deleted file mode 100644 index e97f79f82f..0000000000 --- a/tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php +++ /dev/null @@ -1,59 +0,0 @@ -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 index 0000000000..d85c4e0c3a --- /dev/null +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -0,0 +1,59 @@ +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()); + } +} From d9af35766325a96726af97a12213cb31fa9c7d0f Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 10 Jan 2025 21:58:21 +0000 Subject: [PATCH 07/18] Create LegacyLoggerFactory --- .../Logger/Factory/LegacyLoggerFactory.php | 33 +++++++++++++++++++ src/Core/Logger/Factory/LoggerFactory.php | 28 ++++++++++++++++ .../Factory/LegacyLoggerFactoryTest.php | 29 ++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/Core/Logger/Factory/LegacyLoggerFactory.php create mode 100644 src/Core/Logger/Factory/LoggerFactory.php create mode 100644 tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php diff --git a/src/Core/Logger/Factory/LegacyLoggerFactory.php b/src/Core/Logger/Factory/LegacyLoggerFactory.php new file mode 100644 index 0000000000..42103127e3 --- /dev/null +++ b/src/Core/Logger/Factory/LegacyLoggerFactory.php @@ -0,0 +1,33 @@ +assertInstanceOf( + LoggerInterface::class, + $factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT) + ); + } +} From 48154a090a56be72bc0b26d00dfd0f2451bac03f Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 11 Jan 2025 08:05:50 +0000 Subject: [PATCH 08/18] Create LoggerManager::changeLogChannel() --- src/App.php | 5 +++ src/Core/Logger/LoggerManager.php | 27 ++++++++++---- static/dependencies.config.php | 7 ++++ tests/Unit/Core/Logger/LoggerManagerTest.php | 38 ++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/App.php b/src/App.php index 487707f9fd..8af3219a65 100644 --- a/src/App.php +++ b/src/App.php @@ -19,6 +19,7 @@ use Friendica\Capabilities\ICanHandleRequests; use Friendica\Content\Nav; use Friendica\Core\Config\Factory\Config; use Friendica\Core\Container; +use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Definition\DbaDefinition; @@ -136,6 +137,10 @@ class App $this->container->setup(LogChannel::APP, false); + /** @var LoggerManager */ + $loggerManager = $this->container->create(LoggerManager::class); + $loggerManager->changeLogChannel(LogChannel::APP); + $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); $this->config = $this->container->create(IManageConfigValues::class); diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index 4524e80f59..35d6de1467 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -22,17 +22,23 @@ use Psr\Log\NullLogger; */ final class LoggerManager { + /** + * Workaround: $logger must be static + * because Dice always creates a new LoggerManager object + * + * @var LoggerInterface|null + */ + private static $logger = null; + private IManageConfigValues $config; private bool $debug; private string $logLevel; - private string $logChannel; - private bool $profiling; - private LoggerInterface $logger; + private string $logChannel; public function __construct(IManageConfigValues $config) { @@ -40,8 +46,15 @@ final class LoggerManager $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; + $this->logChannel = LogChannel::DEFAULT; + } + + public function changeLogChannel(string $logChannel): void + { + $this->logChannel = $logChannel; + + self::$logger = null; } /** @@ -49,11 +62,11 @@ final class LoggerManager */ public function getLogger(): LoggerInterface { - if (! isset($this->logger)) { - $this->logger = $this->createProfiledLogger(); + if (self::$logger === null) { + self::$logger = $this->createProfiledLogger(); } - return $this->logger; + return self::$logger; } private function createProfiledLogger(): LoggerInterface diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 28ae277c21..a2c12426b6 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -162,6 +162,13 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo ['create', [], Dice::CHAIN_CALL], ], ], + '$LoggerInterface' => [ + 'shared' => false, + 'instanceOf' => \Friendica\Core\Logger\LoggerManager::class, + 'call' => [ + ['getLogger', [], Dice::CHAIN_CALL], + ], + ], \Friendica\Core\Logger\Type\SyslogLogger::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, 'call' => [ diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index d85c4e0c3a..3fc5ae1abf 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Friendica\Test\Unit\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\Type\ProfilerLogger; use PHPUnit\Framework\TestCase; @@ -20,6 +21,10 @@ class LoggerManagerTest extends TestCase { public function testGetLoggerReturnsPsrLogger(): void { + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); @@ -27,6 +32,10 @@ class LoggerManagerTest extends TestCase public function testGetLoggerReturnsSameObject(): void { + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); $this->assertSame($factory->getLogger(), $factory->getLogger()); @@ -39,6 +48,10 @@ class LoggerManagerTest extends TestCase ['system', 'debugging', null, false], ]); + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + $factory = new LoggerManager($config); $this->assertInstanceOf(NullLogger::class, $factory->getLogger()); @@ -52,8 +65,33 @@ class LoggerManagerTest extends TestCase ['system', 'profiling', null, true], ]); + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + $factory = new LoggerManager($config); $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); } + + public function testChangeChannelReturnsDifferentLogger(): void + { + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'debugging', null, false], + ['system', 'profiling', null, true], + ]); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + + $factory = new LoggerManager($config); + + $logger1 = $factory->getLogger(); + + $factory->changeLogChannel(LogChannel::CONSOLE); + + $this->assertNotSame($logger1, $factory->getLogger()); + } } From 0804413c41accf02d2338302a564be76b322f6bc Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 11 Jan 2025 21:05:35 +0000 Subject: [PATCH 09/18] LegacyLoggerFactory uses Logger as factory --- src/App.php | 10 +++---- .../Logger/Factory/LegacyLoggerFactory.php | 22 +++++++++++++--- src/Core/Logger/LoggerManager.php | 17 ++++++------ static/dependencies.config.php | 15 ++++++----- .../Factory/LegacyLoggerFactoryTest.php | 9 ++++++- tests/Unit/Core/Logger/LoggerManagerTest.php | 26 +++++++++++++++---- 6 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/App.php b/src/App.php index 0f2a6a76f8..6573db427c 100644 --- a/src/App.php +++ b/src/App.php @@ -146,10 +146,6 @@ class App $this->registerErrorHandler(); - /** @var LoggerManager */ - $loggerManager = $this->container->create(LoggerManager::class); - $loggerManager->changeLogChannel(LogChannel::APP); - $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); $this->config = $this->container->create(IManageConfigValues::class); @@ -251,9 +247,9 @@ class App private function setupContainerForLogger(string $logChannel): void { - $this->container->addRule(LoggerInterface::class, [ - 'constructParams' => [$logChannel], - ]); + /** @var LoggerManager */ + $loggerManager = $this->container->create(LoggerManager::class); + $loggerManager->changeLogChannel($logChannel); } private function setupLegacyServiceLocator(): void diff --git a/src/Core/Logger/Factory/LegacyLoggerFactory.php b/src/Core/Logger/Factory/LegacyLoggerFactory.php index 42103127e3..485f3b09dc 100644 --- a/src/Core/Logger/Factory/LegacyLoggerFactory.php +++ b/src/Core/Logger/Factory/LegacyLoggerFactory.php @@ -9,15 +9,29 @@ declare(strict_types=1); namespace Friendica\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Hooks\Capability\ICanCreateInstances; +use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; -use Psr\Log\NullLogger; /** * Manager for the core logging instances */ final class LegacyLoggerFactory implements LoggerFactory { + private ICanCreateInstances $instanceCreator; + + private IManageConfigValues $config; + + private Profiler $profiler; + + public function __construct(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler) + { + $this->instanceCreator = $instanceCreator; + $this->config = $config; + $this->profiler = $profiler; + } + /** * Creates and returns a PSR-3 Logger instance. * @@ -28,6 +42,8 @@ final class LegacyLoggerFactory implements LoggerFactory */ public function createLogger(string $logLevel, string $logChannel): LoggerInterface { - return new NullLogger(); + $factory = new Logger($logChannel); + + return $factory->create($this->instanceCreator, $this->config, $this->profiler); } } diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index 35d6de1467..05234dea23 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -11,6 +11,7 @@ namespace Friendica\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\Type\ProfilerLogger; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -32,6 +33,8 @@ final class LoggerManager private IManageConfigValues $config; + private LoggerFactory $factory; + private bool $debug; private string $logLevel; @@ -40,9 +43,10 @@ final class LoggerManager private string $logChannel; - public function __construct(IManageConfigValues $config) + public function __construct(IManageConfigValues $config, LoggerFactory $factory) { - $this->config = $config; + $this->config = $config; + $this->factory = $factory; $this->debug = (bool) $config->get('system', 'debugging') ?? false; $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; @@ -71,11 +75,11 @@ final class LoggerManager private function createProfiledLogger(): LoggerInterface { - // Always return NullLogger if debug is disabled + // Always create NullLogger if debug is disabled if ($this->debug === false) { $logger = new NullLogger(); } else { - $logger = $this->createLogger($this->logLevel, $this->logChannel); + $logger = $this->factory->createLogger($this->logLevel, $this->logChannel); } if ($this->profiling === true) { @@ -86,9 +90,4 @@ final class LoggerManager return $logger; } - - private function createLogger(string $logLevel, string $logChannel): LoggerInterface - { - return new NullLogger(); - } } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index d640a7109f..f1c2f4e52e 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -157,18 +157,19 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo ], ], \Psr\Log\LoggerInterface::class => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], - ], - ], - '$LoggerInterface' => [ - 'shared' => false, 'instanceOf' => \Friendica\Core\Logger\LoggerManager::class, 'call' => [ ['getLogger', [], Dice::CHAIN_CALL], ], ], + \Friendica\Core\Logger\LoggerManager::class => [ + 'substitutions' => [ + \Friendica\Core\Logger\Factory\LoggerFactory::class => \Friendica\Core\Logger\Factory\LegacyLoggerFactory::class, + ], + ], + \Friendica\Core\Logger\Factory\LoggerFactory::class => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\LegacyLoggerFactory::class, + ], \Friendica\Core\Logger\Type\SyslogLogger::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, 'call' => [ diff --git a/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php b/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php index d888a66581..9ef920c71f 100644 --- a/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php +++ b/tests/Unit/Core/Logger/Factory/LegacyLoggerFactoryTest.php @@ -9,8 +9,11 @@ declare(strict_types=1); namespace Friendica\Test\Unit\Core\Logger\Factory; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Hooks\Capability\ICanCreateInstances; use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\Factory\LegacyLoggerFactory; +use Friendica\Util\Profiler; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -19,7 +22,11 @@ class LegacyLoggerFactoryTest extends TestCase { public function testCreateLoggerReturnsPsrLogger(): void { - $factory = new LegacyLoggerFactory(); + $factory = new LegacyLoggerFactory( + $this->createStub(ICanCreateInstances::class), + $this->createStub(IManageConfigValues::class), + $this->createStub(Profiler::class), + ); $this->assertInstanceOf( LoggerInterface::class, diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index 3fc5ae1abf..81096eee96 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -11,6 +11,7 @@ namespace Friendica\Test\Unit\Core\Logger; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\Type\ProfilerLogger; use PHPUnit\Framework\TestCase; @@ -25,7 +26,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); + $factory = new LoggerManager( + $this->createStub(IManageConfigValues::class), + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); } @@ -36,7 +40,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($this->createStub(IManageConfigValues::class)); + $factory = new LoggerManager( + $this->createStub(IManageConfigValues::class), + $this->createStub(LoggerFactory::class) + ); $this->assertSame($factory->getLogger(), $factory->getLogger()); } @@ -52,7 +59,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(NullLogger::class, $factory->getLogger()); } @@ -69,7 +79,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); } @@ -86,7 +99,10 @@ class LoggerManagerTest extends TestCase $reflectionProperty->setAccessible(true); $reflectionProperty->setValue(null, null); - $factory = new LoggerManager($config); + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); $logger1 = $factory->getLogger(); From 510f4e02c8994cea9ba89f33ca38c99dce26b548 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 08:21:30 +0000 Subject: [PATCH 10/18] Add support for WorkerLogger decorator in LoggerManager, deprecate core Logger class, deprecate DI::workerLogger() --- src/Core/Logger.php | 6 ++++ src/Core/Logger/LoggerManager.php | 28 ++++++++++++------ src/Core/Worker.php | 30 +++++++++++++------- src/DI.php | 8 ++++++ tests/Unit/Core/Logger/LoggerManagerTest.php | 25 +++++++++++++++- 5 files changed, 76 insertions(+), 21 deletions(-) diff --git a/src/Core/Logger.php b/src/Core/Logger.php index c15271e841..83f26322e4 100644 --- a/src/Core/Logger.php +++ b/src/Core/Logger.php @@ -13,6 +13,8 @@ use Psr\Log\LoggerInterface; /** * Logger functions + * + * @deprecated 2025.02 Use constructor injection or `DI::logger()` instead */ class Logger { @@ -48,6 +50,8 @@ class Logger /** * Enable additional logging for worker usage * + * @deprecated + * * @param string $functionName The worker function, which got called * * @throws \Friendica\Network\HTTPException\InternalServerErrorException @@ -60,6 +64,8 @@ class Logger /** * Disable additional logging for worker usage + * + * @deprecated */ public static function disableWorker() { diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index 05234dea23..b3f23dae01 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -13,6 +13,7 @@ use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\Type\ProfilerLogger; +use Friendica\Core\Logger\Type\WorkerLogger; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -31,6 +32,14 @@ final class LoggerManager */ private static $logger = null; + /** + * Workaround: $logChannel must be static + * because Dice always creates a new LoggerManager object + * + * @var LoggerInterface|null + */ + private static string $logChannel = LogChannel::DEFAULT; + private IManageConfigValues $config; private LoggerFactory $factory; @@ -41,8 +50,6 @@ final class LoggerManager private bool $profiling; - private string $logChannel; - public function __construct(IManageConfigValues $config, LoggerFactory $factory) { $this->config = $config; @@ -51,14 +58,12 @@ final class LoggerManager $this->debug = (bool) $config->get('system', 'debugging') ?? false; $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; $this->profiling = (bool) $config->get('system', 'profiling') ?? false; - $this->logChannel = LogChannel::DEFAULT; } public function changeLogChannel(string $logChannel): void { - $this->logChannel = $logChannel; - - self::$logger = null; + self::$logChannel = $logChannel; + self::$logger = null; } /** @@ -67,19 +72,19 @@ final class LoggerManager public function getLogger(): LoggerInterface { if (self::$logger === null) { - self::$logger = $this->createProfiledLogger(); + self::$logger = $this->createLogger(); } return self::$logger; } - private function createProfiledLogger(): LoggerInterface + private function createLogger(): LoggerInterface { // Always create NullLogger if debug is disabled if ($this->debug === false) { $logger = new NullLogger(); } else { - $logger = $this->factory->createLogger($this->logLevel, $this->logChannel); + $logger = $this->factory->createLogger($this->logLevel, self::$logChannel); } if ($this->profiling === true) { @@ -88,6 +93,11 @@ final class LoggerManager $logger = new ProfilerLogger($logger, $profiler); } + // Decorate Logger as WorkerLogger for BC + if (self::$logChannel === LogChannel::WORKER) { + $logger = new WorkerLogger($logger); + } + return $logger; } } diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 568fa2873f..a3252112c3 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -8,6 +8,8 @@ namespace Friendica\Core; use Friendica\Core\Cache\Enum\Duration; +use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Type\WorkerLogger; use Friendica\Core\Worker\Entity\Process; use Friendica\Database\DBA; use Friendica\DI; @@ -537,9 +539,15 @@ class Worker self::coolDown(); - Logger::enableWorker($funcname); + DI::loggerManager()->changeLogChannel(LogChannel::WORKER); - Logger::info('Process start.', ['priority' => $queue['priority'], 'id' => $queue['id']]); + $logger = DI::logger(); + + if ($logger instanceOf WorkerLogger) { + $logger->setFunctionName($funcname); + } + + DI::logger()->info('Process start.', ['priority' => $queue['priority'], 'id' => $queue['id']]); $stamp = (float)microtime(true); @@ -559,19 +567,19 @@ class Worker try { call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv); } catch (\Throwable $e) { - Logger::error('Uncaught exception in worker method execution', ['class' => get_class($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile() . ':' . $e->getLine(), 'trace' => $e->getTraceAsString(), 'previous' => $e->getPrevious()]); + DI::logger()->error('Uncaught exception in worker method execution', ['class' => get_class($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile() . ':' . $e->getLine(), 'trace' => $e->getTraceAsString(), 'previous' => $e->getPrevious()]); Worker::defer(); } } else { try { $funcname($argv, count($argv)); } catch (\Throwable $e) { - Logger::error('Uncaught exception in worker execution', ['message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile() . ':' . $e->getLine(), 'trace' => $e->getTraceAsString(), 'previous' => $e->getPrevious()]); + DI::logger()->error('Uncaught exception in worker execution', ['message' => $e->getMessage(), 'code' => $e->getCode(), 'file' => $e->getFile() . ':' . $e->getLine(), 'trace' => $e->getTraceAsString(), 'previous' => $e->getPrevious()]); Worker::defer(); } } - Logger::disableWorker(); + DI::loggerManager()->changeLogChannel(LogChannel::DEFAULT); $appHelper->setQueue([]); @@ -591,7 +599,7 @@ class Worker $rest = round(max(0, $up_duration - (self::$db_duration + self::$lock_duration)), 2); $exec = round($duration, 2); - Logger::info('Performance:', ['function' => $funcname, 'state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]); + DI::logger()->info('Performance:', ['function' => $funcname, 'state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]); self::coolDown(); @@ -603,16 +611,16 @@ class Worker self::$lock_duration = 0; if ($duration > 3600) { - Logger::info('Longer than 1 hour.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); + DI::logger()->info('Longer than 1 hour.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); } elseif ($duration > 600) { - Logger::info('Longer than 10 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); + DI::logger()->info('Longer than 10 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); } elseif ($duration > 300) { - Logger::info('Longer than 5 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); + DI::logger()->info('Longer than 5 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); } elseif ($duration > 120) { - Logger::info('Longer than 2 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); + DI::logger()->info('Longer than 2 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration / 60, 3)]); } - Logger::info('Process done.', ['function' => $funcname, 'priority' => $queue['priority'], 'retrial' => $queue['retrial'], 'id' => $queue['id'], 'duration' => round($duration, 3)]); + DI::logger()->info('Process done.', ['function' => $funcname, 'priority' => $queue['priority'], 'retrial' => $queue['retrial'], 'id' => $queue['id'], 'duration' => round($duration, 3)]); DI::profiler()->saveLog(DI::logger(), 'ID ' . $queue['id'] . ': ' . $funcname); } diff --git a/src/DI.php b/src/DI.php index 72be1f3aaa..7d1114ac5c 100644 --- a/src/DI.php +++ b/src/DI.php @@ -9,6 +9,7 @@ namespace Friendica; use Dice\Dice; use Friendica\Core\Logger\Capability\ICheckLoggerSettings; +use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\Util\LoggerSettingsCheck; use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; @@ -324,6 +325,8 @@ abstract class DI } /** + * @deprecated 2025.02 Use `DI::loggerManager()` and `DI::logger()` instead + * * @return \Friendica\Core\Logger\Type\WorkerLogger */ public static function workerLogger() @@ -331,6 +334,11 @@ abstract class DI return self::$dice->create(Core\Logger\Type\WorkerLogger::class); } + public static function loggerManager(): LoggerManager + { + return self::$dice->create(LoggerManager::class); + } + // // "Factory" namespace instances // diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index 81096eee96..14db1c2676 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -14,6 +14,7 @@ use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\Factory\LoggerFactory; use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\Type\ProfilerLogger; +use Friendica\Core\Logger\Type\WorkerLogger; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -87,7 +88,7 @@ class LoggerManagerTest extends TestCase $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); } - public function testChangeChannelReturnsDifferentLogger(): void + public function testChangeLogChannelReturnsDifferentLogger(): void { $config = $this->createStub(IManageConfigValues::class); $config->method('get')->willReturnMap([ @@ -110,4 +111,26 @@ class LoggerManagerTest extends TestCase $this->assertNotSame($logger1, $factory->getLogger()); } + + public function testChangeLogChannelToWorkerReturnsWorkerLogger(): void + { + $config = $this->createStub(IManageConfigValues::class); + $config->method('get')->willReturnMap([ + ['system', 'debugging', null, false], + ['system', 'profiling', null, true], + ]); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + + $factory = new LoggerManager( + $config, + $this->createStub(LoggerFactory::class) + ); + + $factory->changeLogChannel(LogChannel::WORKER); + + $this->assertInstanceOf(WorkerLogger::class, $factory->getLogger()); + } } From 7ebb57c82b5fbb1942b5daf3997d3b9cdbfb09f0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 08:23:45 +0000 Subject: [PATCH 11/18] Fix code style --- src/Core/Logger/LoggerManager.php | 6 +++--- src/Core/Worker.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index b3f23dae01..c60bce660f 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -55,9 +55,9 @@ final class LoggerManager $this->config = $config; $this->factory = $factory; - $this->debug = (bool) $config->get('system', 'debugging') ?? false; - $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; - $this->profiling = (bool) $config->get('system', 'profiling') ?? false; + $this->debug = (bool) $config->get('system', 'debugging') ?? false; + $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; + $this->profiling = (bool) $config->get('system', 'profiling') ?? false; } public function changeLogChannel(string $logChannel): void diff --git a/src/Core/Worker.php b/src/Core/Worker.php index a3252112c3..c84de94f63 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -543,7 +543,7 @@ class Worker $logger = DI::logger(); - if ($logger instanceOf WorkerLogger) { + if ($logger instanceof WorkerLogger) { $logger->setFunctionName($funcname); } From 2f811f99de9875cd717877bc1d884d84f1c35508 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 13:04:49 +0000 Subject: [PATCH 12/18] Fix PHPStan error, fix code style --- src/Core/Logger/LoggerManager.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php index c60bce660f..697756099d 100644 --- a/src/Core/Logger/LoggerManager.php +++ b/src/Core/Logger/LoggerManager.php @@ -35,8 +35,6 @@ final class LoggerManager /** * Workaround: $logChannel must be static * because Dice always creates a new LoggerManager object - * - * @var LoggerInterface|null */ private static string $logChannel = LogChannel::DEFAULT; @@ -55,9 +53,9 @@ final class LoggerManager $this->config = $config; $this->factory = $factory; - $this->debug = (bool) $config->get('system', 'debugging') ?? false; + $this->debug = (bool) $config->get('system', 'debugging') ?? false; $this->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE; - $this->profiling = (bool) $config->get('system', 'profiling') ?? false; + $this->profiling = (bool) $config->get('system', 'profiling') ?? false; } public function changeLogChannel(string $logChannel): void From 9d09b105375ae57b882a7fbc810d0f893e543e46 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 13:07:05 +0000 Subject: [PATCH 13/18] Remove unused Friendica\Core\Logger::enableWorker() and disableWorker() --- src/Core/Logger.php | 48 +-------------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/src/Core/Logger.php b/src/Core/Logger.php index 83f26322e4..a87be78633 100644 --- a/src/Core/Logger.php +++ b/src/Core/Logger.php @@ -18,58 +18,12 @@ use Psr\Log\LoggerInterface; */ class Logger { - /** - * LoggerInterface The default Logger type - * - * @var string - */ - const TYPE_LOGGER = LoggerInterface::class; - /** - * WorkerLogger A specific worker logger type, which can be enabled - * - * @var string - */ - const TYPE_WORKER = WorkerLogger::class; - /** - * @var string $type LoggerInterface The current logger type - */ - private static $type = self::TYPE_LOGGER; - /** * @return LoggerInterface|WorkerLogger */ private static function getInstance() { - if (self::$type === self::TYPE_LOGGER) { - return DI::logger(); - } else { - return DI::workerLogger(); - } - } - - /** - * Enable additional logging for worker usage - * - * @deprecated - * - * @param string $functionName The worker function, which got called - * - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - public static function enableWorker(string $functionName) - { - self::$type = self::TYPE_WORKER; - DI::workerLogger()->setFunctionName($functionName); - } - - /** - * Disable additional logging for worker usage - * - * @deprecated - */ - public static function disableWorker() - { - self::$type = self::TYPE_LOGGER; + return DI::logger(); } /** From ea35ec4d6e9967f64a57d08483147be4d4e4e96b Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 13:19:47 +0000 Subject: [PATCH 14/18] Rename method to setup logger --- src/App.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/App.php b/src/App.php index 6573db427c..3ffce9114c 100644 --- a/src/App.php +++ b/src/App.php @@ -140,7 +140,7 @@ class App $this->setupContainerForAddons(); - $this->setupContainerForLogger(LogChannel::APP); + $this->setupLogChannel(LogChannel::APP); $this->setupLegacyServiceLocator(); @@ -182,7 +182,7 @@ class App { $this->setupContainerForAddons(); - $this->setupContainerForLogger($this->determineLogChannel($argv)); + $this->setupLogChannel($this->determineLogChannel($argv)); $this->setupLegacyServiceLocator(); @@ -197,7 +197,7 @@ class App { $this->setupContainerForAddons(); - $this->setupContainerForLogger(LogChannel::AUTH_JABBERED); + $this->setupLogChannel(LogChannel::AUTH_JABBERED); $this->setupLegacyServiceLocator(); @@ -245,7 +245,7 @@ class App return LogChannel::CONSOLE; } - private function setupContainerForLogger(string $logChannel): void + private function setupLogChannel(string $logChannel): void { /** @var LoggerManager */ $loggerManager = $this->container->create(LoggerManager::class); From 148c9cdfe6a4908b728918aa98a211946945160e Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 12 Jan 2025 13:20:01 +0000 Subject: [PATCH 15/18] Fix PHP warning --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 3ffce9114c..73f2913b1a 100644 --- a/src/App.php +++ b/src/App.php @@ -230,7 +230,7 @@ class App private function determineLogChannel(array $argv): string { - $command = strtolower($argv[1]) ?? ''; + $command = strtolower($argv[1] ?? ''); if ($command === 'daemon' || $command === 'jetstream') { return LogChannel::DAEMON; From c1dde29ef696291226fab77790d94a523c200da5 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 13 Jan 2025 15:17:20 +0000 Subject: [PATCH 16/18] Add description for LegacyLoggerFactory --- src/Core/Logger/Factory/LegacyLoggerFactory.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Core/Logger/Factory/LegacyLoggerFactory.php b/src/Core/Logger/Factory/LegacyLoggerFactory.php index 485f3b09dc..5c91d11771 100644 --- a/src/Core/Logger/Factory/LegacyLoggerFactory.php +++ b/src/Core/Logger/Factory/LegacyLoggerFactory.php @@ -15,7 +15,17 @@ use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; /** - * Manager for the core logging instances + * Bridge for the legacy Logger factory. + * + * This class can be removed after the following classes are replaced or + * refactored implementing the `\Friendica\Core\Logger\Factory\LoggerFactory`: + * + * - Friendica\Core\Logger\Factory\StreamLogger + * - Friendica\Core\Logger\Factory\SyslogLogger + * - monolog addon: Friendica\Addon\monolog\src\Factory\Monolog + * + * @see \Friendica\Core\Logger\Factory\StreamLogger + * @see \Friendica\Core\Logger\Factory\SyslogLogger */ final class LegacyLoggerFactory implements LoggerFactory { From 39088ab003b2af862209ba777276577d6325619c Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 13 Jan 2025 16:41:35 +0000 Subject: [PATCH 17/18] Clean static private properties of LoggerManager after test --- tests/Unit/Core/Logger/LoggerManagerTest.php | 48 ++++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index 14db1c2676..0dae2fcc6c 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -23,30 +23,30 @@ class LoggerManagerTest extends TestCase { public function testGetLoggerReturnsPsrLogger(): void { - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $this->createStub(IManageConfigValues::class), $this->createStub(LoggerFactory::class) ); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } public function testGetLoggerReturnsSameObject(): void { - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $this->createStub(IManageConfigValues::class), $this->createStub(LoggerFactory::class) ); $this->assertSame($factory->getLogger(), $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } public function testGetLoggerWithDebugDisabledReturnsNullLogger(): void @@ -56,16 +56,16 @@ class LoggerManagerTest extends TestCase ['system', 'debugging', null, false], ]); - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $config, $this->createStub(LoggerFactory::class) ); $this->assertInstanceOf(NullLogger::class, $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } public function testGetLoggerWithProfilerEnabledReturnsProfilerLogger(): void @@ -76,16 +76,16 @@ class LoggerManagerTest extends TestCase ['system', 'profiling', null, true], ]); - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $config, $this->createStub(LoggerFactory::class) ); $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } public function testChangeLogChannelReturnsDifferentLogger(): void @@ -96,10 +96,6 @@ class LoggerManagerTest extends TestCase ['system', 'profiling', null, true], ]); - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $config, $this->createStub(LoggerFactory::class) @@ -110,6 +106,10 @@ class LoggerManagerTest extends TestCase $factory->changeLogChannel(LogChannel::CONSOLE); $this->assertNotSame($logger1, $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } public function testChangeLogChannelToWorkerReturnsWorkerLogger(): void @@ -120,10 +120,6 @@ class LoggerManagerTest extends TestCase ['system', 'profiling', null, true], ]); - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); - $factory = new LoggerManager( $config, $this->createStub(LoggerFactory::class) @@ -132,5 +128,9 @@ class LoggerManagerTest extends TestCase $factory->changeLogChannel(LogChannel::WORKER); $this->assertInstanceOf(WorkerLogger::class, $factory->getLogger()); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); } } From f9b7b6a41358355e4af4a8340f19cd897ca59065 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 13 Jan 2025 16:52:05 +0000 Subject: [PATCH 18/18] Clean private static properties of LoggerManager via tearDown method --- tests/Unit/Core/Logger/LoggerManagerTest.php | 41 ++++++++------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/tests/Unit/Core/Logger/LoggerManagerTest.php b/tests/Unit/Core/Logger/LoggerManagerTest.php index 0dae2fcc6c..02f9c0b2e2 100644 --- a/tests/Unit/Core/Logger/LoggerManagerTest.php +++ b/tests/Unit/Core/Logger/LoggerManagerTest.php @@ -21,6 +21,23 @@ use Psr\Log\NullLogger; class LoggerManagerTest extends TestCase { + /** + * Clean the private static properties + * + * @see LoggerManager::$logger + * @see LoggerManager::$logChannel + */ + protected function tearDown(): void + { + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, null); + + $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logChannel'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null, LogChannel::DEFAULT); + } + public function testGetLoggerReturnsPsrLogger(): void { $factory = new LoggerManager( @@ -29,10 +46,6 @@ class LoggerManagerTest extends TestCase ); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } public function testGetLoggerReturnsSameObject(): void @@ -43,10 +56,6 @@ class LoggerManagerTest extends TestCase ); $this->assertSame($factory->getLogger(), $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } public function testGetLoggerWithDebugDisabledReturnsNullLogger(): void @@ -62,10 +71,6 @@ class LoggerManagerTest extends TestCase ); $this->assertInstanceOf(NullLogger::class, $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } public function testGetLoggerWithProfilerEnabledReturnsProfilerLogger(): void @@ -82,10 +87,6 @@ class LoggerManagerTest extends TestCase ); $this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } public function testChangeLogChannelReturnsDifferentLogger(): void @@ -106,10 +107,6 @@ class LoggerManagerTest extends TestCase $factory->changeLogChannel(LogChannel::CONSOLE); $this->assertNotSame($logger1, $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } public function testChangeLogChannelToWorkerReturnsWorkerLogger(): void @@ -128,9 +125,5 @@ class LoggerManagerTest extends TestCase $factory->changeLogChannel(LogChannel::WORKER); $this->assertInstanceOf(WorkerLogger::class, $factory->getLogger()); - - $reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null, null); } }