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()); + } }