change Monolog logger to a configurable instance

This commit is contained in:
Daniel Seifert 2022-08-02 23:01:23 +02:00
parent 00b36364e1
commit 210920cfaa
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
5 changed files with 13 additions and 305 deletions

View File

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

View File

@ -1,242 +0,0 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @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);
}
}

View File

@ -1,23 +0,0 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DebugBar\Application\Core;
use OxidEsales\Eshop\Core\Exception\StandardException;
class LoggerNotSetException extends StandardException
{
}

View File

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

9
services.yaml Normal file
View File

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