Create LoggerManager::changeLogChannel()

This commit is contained in:
Art4 2025-01-11 08:05:50 +00:00
parent d9af357663
commit 48154a090a
4 changed files with 70 additions and 7 deletions

View file

@ -19,6 +19,7 @@ use Friendica\Capabilities\ICanHandleRequests;
use Friendica\Content\Nav; use Friendica\Content\Nav;
use Friendica\Core\Config\Factory\Config; use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Container; use Friendica\Core\Container;
use Friendica\Core\Logger\LoggerManager;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Definition\DbaDefinition; use Friendica\Database\Definition\DbaDefinition;
@ -136,6 +137,10 @@ class App
$this->container->setup(LogChannel::APP, false); $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->requestId = $this->container->create(Request::class)->getRequestId();
$this->auth = $this->container->create(Authentication::class); $this->auth = $this->container->create(Authentication::class);
$this->config = $this->container->create(IManageConfigValues::class); $this->config = $this->container->create(IManageConfigValues::class);

View file

@ -22,17 +22,23 @@ use Psr\Log\NullLogger;
*/ */
final class LoggerManager 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 IManageConfigValues $config;
private bool $debug; private bool $debug;
private string $logLevel; private string $logLevel;
private string $logChannel;
private bool $profiling; private bool $profiling;
private LoggerInterface $logger; private string $logChannel;
public function __construct(IManageConfigValues $config) public function __construct(IManageConfigValues $config)
{ {
@ -40,8 +46,15 @@ final class LoggerManager
$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->logLevel = (string) $config->get('system', 'loglevel') ?? LogLevel::NOTICE;
$this->logChannel = LogChannel::DEFAULT;
$this->profiling = (bool) $config->get('system', 'profiling') ?? false; $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 public function getLogger(): LoggerInterface
{ {
if (! isset($this->logger)) { if (self::$logger === null) {
$this->logger = $this->createProfiledLogger(); self::$logger = $this->createProfiledLogger();
} }
return $this->logger; return self::$logger;
} }
private function createProfiledLogger(): LoggerInterface private function createProfiledLogger(): LoggerInterface

View file

@ -162,6 +162,13 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo
['create', [], Dice::CHAIN_CALL], ['create', [], Dice::CHAIN_CALL],
], ],
], ],
'$LoggerInterface' => [
'shared' => false,
'instanceOf' => \Friendica\Core\Logger\LoggerManager::class,
'call' => [
['getLogger', [], Dice::CHAIN_CALL],
],
],
\Friendica\Core\Logger\Type\SyslogLogger::class => [ \Friendica\Core\Logger\Type\SyslogLogger::class => [
'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class,
'call' => [ 'call' => [

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Logger; namespace Friendica\Test\Unit\Core\Logger;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\LoggerManager; use Friendica\Core\Logger\LoggerManager;
use Friendica\Core\Logger\Type\ProfilerLogger; use Friendica\Core\Logger\Type\ProfilerLogger;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -20,6 +21,10 @@ class LoggerManagerTest extends TestCase
{ {
public function testGetLoggerReturnsPsrLogger(): void public function testGetLoggerReturnsPsrLogger(): void
{ {
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, null);
$factory = new LoggerManager($this->createStub(IManageConfigValues::class)); $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
$this->assertInstanceOf(LoggerInterface::class, $factory->getLogger()); $this->assertInstanceOf(LoggerInterface::class, $factory->getLogger());
@ -27,6 +32,10 @@ class LoggerManagerTest extends TestCase
public function testGetLoggerReturnsSameObject(): void public function testGetLoggerReturnsSameObject(): void
{ {
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, null);
$factory = new LoggerManager($this->createStub(IManageConfigValues::class)); $factory = new LoggerManager($this->createStub(IManageConfigValues::class));
$this->assertSame($factory->getLogger(), $factory->getLogger()); $this->assertSame($factory->getLogger(), $factory->getLogger());
@ -39,6 +48,10 @@ class LoggerManagerTest extends TestCase
['system', 'debugging', null, false], ['system', 'debugging', null, false],
]); ]);
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, null);
$factory = new LoggerManager($config); $factory = new LoggerManager($config);
$this->assertInstanceOf(NullLogger::class, $factory->getLogger()); $this->assertInstanceOf(NullLogger::class, $factory->getLogger());
@ -52,8 +65,33 @@ class LoggerManagerTest extends TestCase
['system', 'profiling', null, true], ['system', 'profiling', null, true],
]); ]);
$reflectionProperty = new \ReflectionProperty(LoggerManager::class, 'logger');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, null);
$factory = new LoggerManager($config); $factory = new LoggerManager($config);
$this->assertInstanceOf(ProfilerLogger::class, $factory->getLogger()); $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());
}
} }