LegacyLoggerFactory uses Logger as factory

This commit is contained in:
Art4 2025-01-11 21:05:35 +00:00
parent b40db06ef3
commit 0804413c41
6 changed files with 67 additions and 32 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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