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\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);

View file

@ -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

View file

@ -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' => [

View file

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