From 210920cfaaad744c6d98c9852a1fb366a1a53fa4 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 2 Aug 2022 23:01:23 +0200 Subject: [PATCH] change Monolog logger to a configurable instance --- Application/Component/DebugBarComponent.php | 21 +- Application/Core/LoggerCascade.php | 242 -------------------- Application/Core/LoggerNotSetException.php | 23 -- Modules/functions.php | 23 -- services.yaml | 9 + 5 files changed, 13 insertions(+), 305 deletions(-) delete mode 100644 Application/Core/LoggerCascade.php delete mode 100644 Application/Core/LoggerNotSetException.php create mode 100644 services.yaml diff --git a/Application/Component/DebugBarComponent.php b/Application/Component/DebugBarComponent.php index d97f231..60e0c24 100644 --- a/Application/Component/DebugBarComponent.php +++ b/Application/Component/DebugBarComponent.php @@ -15,8 +15,6 @@ declare(strict_types=1); namespace D3\DebugBar\Application\Component; -use D3\DebugBar\Application\Core\LoggerCascade; -use D3\DebugBar\Application\Core\LoggerNotSetException; use D3\DebugBar\Application\Models\Collectors\SmartyCollector; use D3\DebugBar\Application\Models\TimeDataCollectorHandler; use DebugBar\Bridge\DoctrineCollector; @@ -69,23 +67,11 @@ class DebugBarComponent extends BaseController /** * @return MonologCollector - * @throws ReflectionException */ public function getMonologCollector(): MonologCollector { - $logger = Registry::getLogger(); - - try { - if ( $logger instanceof LoggerCascade ) { - $monolog = $logger->getLogger( LoggerCascade::DEBUGBAR_LOGGER ); - } else { - /** @var Logger $monolog */ - $monolog = $this->getNonPublicProperty( $logger, 'logger' ); - } - } catch ( LoggerNotSetException $e ) { - $monolog = new Logger( 'nullLogger' ); - } - + /** @var Logger $monolog */ + $monolog = Registry::getLogger(); return new MonologCollector($monolog); } @@ -109,7 +95,8 @@ class DebugBarComponent extends BaseController */ public function getSmartyCollector(): SmartyCollector { - return new SmartyCollector(Registry::getUtilsView()->getSmarty()); + $smarty = Registry::getUtilsView()->getSmarty(); + return new SmartyCollector($smarty); } /** diff --git a/Application/Core/LoggerCascade.php b/Application/Core/LoggerCascade.php deleted file mode 100644 index ec3aacc..0000000 --- a/Application/Core/LoggerCascade.php +++ /dev/null @@ -1,242 +0,0 @@ - - * @link https://www.oxidmodule.com - */ - -declare(strict_types=1); - -namespace D3\DebugBar\Application\Core; - -use InvalidArgumentException; -use Monolog\Logger; -use Psr\Log\LoggerInterface; -use Stringable; - -class LoggerCascade implements LoggerInterface -{ - public const OXID_LOGGER = 'oxidLogger'; - public const DEBUGBAR_LOGGER = 'debugBarLogger'; - - /** - * List of all loggers in the registry (by named indexes) - * - * @var Logger[] - */ - private $loggers = array(); - - /** - * Adds new logging channel to the registry - * - * @param LoggerInterface $logger Instance of the logging channel - * @param string $name Name of the logging channel ($logger->getName() by default) - * @param bool $overwrite Overwrite instance in the registry if the given name already exists? - * @throws InvalidArgumentException If $overwrite set to false and named Logger instance already exists - * @return void - */ - public function addLogger(LoggerInterface $logger, $name, $overwrite = false): void - { - if (isset($this->loggers[$name]) && !$overwrite) { - throw new InvalidArgumentException('Logger with the given name already exists'); - } - - $this->loggers[$name] = $logger; - } - - /** - * Checks if such logging channel exists by name or instance - * - * @param string|Logger $logger Name or logger instance - * @return bool - */ - public function hasLogger($logger): bool - { - if ($logger instanceof LoggerInterface) { - $index = array_search($logger, $this->loggers, true); - - return false !== $index; - } else { - return isset($this->loggers[$logger]); - } - } - - /** - * Removes instance from registry by name or instance - * - * @param string|Logger $logger Name or logger instance - * @return void - */ - public function removeLogger($logger): void - { - if ($logger instanceof LoggerInterface) { - if (false !== ($idx = array_search($logger, $this->loggers, true))) { - unset($this->loggers[$idx]); - } - } else { - unset($this->loggers[$logger]); - } - } - - /** - * Clears the registry - * @return void - */ - public function clear(): void - { - $this->loggers = array(); - } - - /** - * - * @return Logger[] - */ - public function getLoggers(): array - { - return $this->loggers; - } - - /** - * @param $logger - * - * @return LoggerInterface - * @throws LoggerNotSetException - */ - public function getLogger($logger): LoggerInterface - { - if ($this->hasLogger($logger)) { - if ($logger instanceof LoggerInterface) { - $index = array_search($logger, $this->loggers, true); - - return $this->getLoggers()[$index]; - } else { - return $this->getLoggers()[$logger]; - } - } - - throw oxNew(LoggerNotSetException::class); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function emergency($message, array $context = [] ): bool - { - return $this->call('emergency', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function alert($message, array $context = [] ): bool - { - return $this->call('alert', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function critical($message, array $context = [] ): bool - { - return $this->call('critical', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function error($message, array $context = [] ): bool - { - return $this->call('error', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function warning($message, array $context = [] ): bool - { - return $this->call('warning', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function notice($message, array $context = [] ): bool - { - return $this->call('notice', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function info($message, array $context = [] ): bool - { - return $this->call('info', $message, $context); - } - - /** - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function debug($message, array $context = [] ): bool - { - return $this->call('debug', $message, $context); - } - - /** - * @param mixed $level - * @param string|Stringable $message - * @param array $context - * - * @return bool - */ - public function log( $level, $message, array $context = [] ): bool - { - return $this->call('log', $level, $message, $context); - } - - /** - * @param $method - * @param ...$arguments - * - * @return bool - */ - protected function call($method, ...$arguments): bool - { - $return = []; - - foreach ($this->getLoggers() as $logger) { - $return[] = call_user_func_array([$logger, $method], $arguments); - } - - return false === in_array(false, $return); - } -} \ No newline at end of file diff --git a/Application/Core/LoggerNotSetException.php b/Application/Core/LoggerNotSetException.php deleted file mode 100644 index a68f1f0..0000000 --- a/Application/Core/LoggerNotSetException.php +++ /dev/null @@ -1,23 +0,0 @@ - - * @link https://www.oxidmodule.com - */ - -declare(strict_types=1); - -namespace D3\DebugBar\Application\Core; - -use OxidEsales\Eshop\Core\Exception\StandardException; - -class LoggerNotSetException extends StandardException -{ - -} \ No newline at end of file diff --git a/Modules/functions.php b/Modules/functions.php index aa1a086..e76d0c4 100644 --- a/Modules/functions.php +++ b/Modules/functions.php @@ -13,13 +13,7 @@ declare(strict_types=1); -use D3\DebugBar\Application\Core\LoggerCascade; use D3\DebugBar\Application\Models\TimeDataCollectorHandler; -use Monolog\Logger; -use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; -use Psr\Log\LoggerInterface; function startProfile($sProfileName) { @@ -62,21 +56,4 @@ function stopProfile($sProfileName) } $executionCounts[$sProfileName]++; $aStartTimes[$sProfileName] = microtime(true); -} - -/** - * @return LoggerCascade - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ -function getLogger(): LoggerCascade -{ - $container = ContainerFactory::getInstance()->getContainer(); - $logger = $container->get( LoggerInterface::class); - - $cascade = new LoggerCascade(); - $cascade->addLogger($logger, LoggerCascade::OXID_LOGGER); - $cascade->addLogger(new Logger(LoggerCascade::DEBUGBAR_LOGGER), LoggerCascade::DEBUGBAR_LOGGER); - - return $cascade; } \ No newline at end of file diff --git a/services.yaml b/services.yaml new file mode 100644 index 0000000..f4e7697 --- /dev/null +++ b/services.yaml @@ -0,0 +1,9 @@ +services: + _defaults: + autowire: true + public: false + + Psr\Log\LoggerInterface: + class: Monolog\Logger + factory: 'OxidEsales\EshopCommunity\Internal\Framework\Logger\Factory\LoggerFactoryInterface:create' + public: true \ No newline at end of file