]> git.mxchange.org Git - friendica.git/commitdiff
LoggerFactory can create ProfilerLogger
authorArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 13:28:42 +0000 (13:28 +0000)
committerArt4 <art4@wlabs.de>
Fri, 10 Jan 2025 13:28:42 +0000 (13:28 +0000)
src/Core/Logger/Factory/LoggerFactory.php
tests/Unit/Core/Logger/Factory/LoggerFactoryTest.php

index f3c725deff4d3665b93b83f771d6ce7376346ea7..37830c32e10121d947112c5a4050b4740ea291eb 100644 (file)
@@ -10,6 +10,8 @@ declare(strict_types=1);
 namespace Friendica\Core\Logger\Factory;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Type\ProfilerLogger;
+use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
 
@@ -18,24 +20,44 @@ use Psr\Log\NullLogger;
  */
 final class LoggerFactory
 {
+       private IManageConfigValues $config;
+
        private bool $debug;
 
+       private bool $profiling;
+
        private LoggerInterface $logger;
 
        public function __construct(IManageConfigValues $config)
        {
-               $this->debug = (bool) $config->get('system', 'debugging') ?? false;
+               $this->config = $config;
+
+               $this->debug     = (bool) $config->get('system', 'debugging') ?? false;
+               $this->profiling = (bool) $config->get('system', 'profiling') ?? false;
        }
 
        public function create(): LoggerInterface
        {
                if (! isset($this->logger)) {
-                       $this->logger = $this->createLogger();
+                       $this->logger = $this->createProfiledLogger();
                }
 
                return $this->logger;
        }
 
+       private function createProfiledLogger(): LoggerInterface
+       {
+               $logger = $this->createLogger();
+
+               if ($this->profiling === true) {
+                       $profiler = new Profiler($this->config);
+
+                       $logger = new ProfilerLogger($logger, $profiler);
+               }
+
+               return $logger;
+       }
+
        private function createLogger(): LoggerInterface
        {
                // Always return NullLogger if debug is disabled
index aa80dffe83bdc196c68928f2aae5f830374751ad..e97f79f82f8ac9e10eb3b38308166a8af0c2b783 100644 (file)
@@ -11,6 +11,7 @@ namespace Friendica\Test\Unit\Core\Logger\Factory;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Logger\Factory\LoggerFactory;
+use Friendica\Core\Logger\Type\ProfilerLogger;
 use PHPUnit\Framework\TestCase;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
@@ -34,10 +35,25 @@ class LoggerFactoryTest extends TestCase
        public function testCreateWithDebugDisabledReturnsNullLogger(): void
        {
                $config = $this->createStub(IManageConfigValues::class);
-               $config->method('get')->willReturn(false);
+               $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());
+       }
 }