From: Art4 Date: Fri, 10 Jan 2025 15:21:56 +0000 (+0000) Subject: Rename LoggerFactory into LoggerManager X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2df76617c17d3a090f582e5bcb214312f7f601bf;p=friendica.git Rename LoggerFactory into LoggerManager --- diff --git a/src/Core/Logger/Factory/LoggerFactory.php b/src/Core/Logger/Factory/LoggerFactory.php deleted file mode 100644 index 5742fdd5b5..0000000000 --- a/src/Core/Logger/Factory/LoggerFactory.php +++ /dev/null @@ -1,78 +0,0 @@ -config = $config; - - $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 - { - if (! isset($this->logger)) { - $this->logger = $this->createProfiledLogger(); - } - - return $this->logger; - } - - private function createProfiledLogger(): LoggerInterface - { - // 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); - - $logger = new ProfilerLogger($logger, $profiler); - } - - return $logger; - } - - private function createLogger(string $logLevel, string $logChannel): LoggerInterface - { - return new NullLogger(); - } -} diff --git a/src/Core/Logger/LoggerManager.php b/src/Core/Logger/LoggerManager.php new file mode 100644 index 0000000000..4524e80f59 --- /dev/null +++ b/src/Core/Logger/LoggerManager.php @@ -0,0 +1,81 @@ +config = $config; + + $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; + } + + /** + * (Creates and) Returns the logger instance + */ + public function getLogger(): LoggerInterface + { + if (! isset($this->logger)) { + $this->logger = $this->createProfiledLogger(); + } + + return $this->logger; + } + + private function createProfiledLogger(): LoggerInterface + { + // 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); + + $logger = new ProfilerLogger($logger, $profiler); + } + + return $logger; + } + + private function createLogger(string $logLevel, string $logChannel): LoggerInterface + { + return new NullLogger(); + } +} 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()); + } +}