can apply processors

This commit is contained in:
Daniel Seifert 2025-02-11 16:42:20 +01:00
bovenliggende 5b18f7314e
commit 1bc0880219
Getekend door: DanielS
GPG sleutel-ID: 6A513E13AEE66170

Bestand weergeven

@ -28,6 +28,7 @@ use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use RuntimeException; use RuntimeException;
@ -45,6 +46,11 @@ class LoggerFactory
public const MAKEUNIQUE_OPTION_LEVEL = 'loglevel'; public const MAKEUNIQUE_OPTION_LEVEL = 'loglevel';
public const MAKEUNIQUE_OPTION_TIME = 'time'; public const MAKEUNIQUE_OPTION_TIME = 'time';
public const PROCESSOR_UNIQUE_ID = 'processorUId';
public const PROCESSOR_FILTERSENSITIVE = 'processorFilterSensitive';
public const FILTERSENSITIVE_SECRETS = 'secrets';
public static function create(): LoggerFactory public static function create(): LoggerFactory
{ {
return new LoggerFactory(); return new LoggerFactory();
@ -55,7 +61,8 @@ class LoggerFactory
* @param string $filePath * @param string $filePath
* @param int $logLevel * @param int $logLevel
* @param int|null $maxFiles * @param int|null $maxFiles
* @param array<int|string, string|array<string, string>> $specialHandlers * @param array<int|string, string|array<string, string>> $specialHandlerFlags
* @param array<int|string, string|array<string, string>> $processorFlags
* @return Logger * @return Logger
* @throws Exception * @throws Exception
*/ */
@ -64,15 +71,21 @@ class LoggerFactory
string $filePath, string $filePath,
int $logLevel = Logger::INFO, int $logLevel = Logger::INFO,
?int $maxFiles = null, ?int $maxFiles = null,
array $specialHandlers = [] array $specialHandlerFlags = [],
array $processorFlags = []
): Logger { ): Logger {
$logger = new Logger($loggerName); $logger = new Logger($loggerName);
$handler = $this->applySpecialHandlers( $handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles), $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlers $specialHandlerFlags
); );
$logger->pushHandler($handler); $logger->pushHandler($handler);
$this->applyProcessors(
$logger,
$processorFlags
);
return $logger; return $logger;
} }
@ -96,7 +109,8 @@ class LoggerFactory
* @param string $filePath * @param string $filePath
* @param int $logLevel * @param int $logLevel
* @param int|null $maxFiles * @param int|null $maxFiles
* @param array<int|string, string|array<string, string>> $specialHandlers * @param array<int|string, string|array<string, string>> $specialHandlerFlags
* @param array<int|string, string|array<string, string>> $processorFlags
* @return Logger * @return Logger
* @throws Exception * @throws Exception
*/ */
@ -105,7 +119,8 @@ class LoggerFactory
string $filePath, string $filePath,
int $logLevel = Logger::INFO, int $logLevel = Logger::INFO,
?int $maxFiles = null, ?int $maxFiles = null,
array $specialHandlers = [] array $specialHandlerFlags = [],
array $processorFlags = []
): Logger { ): Logger {
if (!class_exists(Registry::class)) { if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
@ -114,21 +129,26 @@ class LoggerFactory
$logger = new Logger($loggerName); $logger = new Logger($loggerName);
$handler = $this->applySpecialHandlers( $handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles), $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlers $specialHandlerFlags
); );
$logger->pushHandler($handler); $logger->pushHandler($handler);
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log'); $oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
$oxidHandler = $this->applySpecialHandlers( $oxidHandler = $this->applySpecialHandlers(
new StreamHandler($oxidLogFilePath, Logger::ERROR), new StreamHandler($oxidLogFilePath, Logger::ERROR),
$specialHandlers $specialHandlerFlags
); );
$logger->pushHandler($oxidHandler); $logger->pushHandler($oxidHandler);
$this->applyProcessors(
$logger,
$processorFlags
);
return $logger; return $logger;
} }
protected function getOxidLogPath(string $fileName): string public function getOxidLogPath(string $fileName): string
{ {
if (!class_exists(Registry::class)) { if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
@ -139,17 +159,17 @@ class LoggerFactory
/** /**
* @param AbstractProcessingHandler $handler * @param AbstractProcessingHandler $handler
* @param array<int|string, string|array<string, string|int>> $specialHandlers * @param array<int|string, string|array<string, string|int>> $specialHandlerFlags
* @return HandlerInterface * @return HandlerInterface
*/ */
public function applySpecialHandlers( public function applySpecialHandlers(
AbstractProcessingHandler $handler, AbstractProcessingHandler $handler,
array $specialHandlers = [] array $specialHandlerFlags = []
): HandlerInterface { ): HandlerInterface {
if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlers, true)) { if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlerFlags, true)) {
$handler = $this->setBuffering($handler); $handler = $this->setBuffering($handler);
} elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlers), true)) { } elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlerFlags), true)) {
$options = $specialHandlers[self::SPECIAL_HANDLERS_BUFFERING]; $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_BUFFERING];
$handler = $this->setBuffering( $handler = $this->setBuffering(
$handler, $handler,
/** @phpstan-ignore argument.type */ /** @phpstan-ignore argument.type */
@ -159,10 +179,10 @@ class LoggerFactory
); );
} }
if (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, $specialHandlers, true)) { if (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, $specialHandlerFlags, true)) {
$handler = $this->setLogItemsOnErrorOnly($handler); $handler = $this->setLogItemsOnErrorOnly($handler);
} elseif (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, array_keys($specialHandlers), true)) { } elseif (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, array_keys($specialHandlerFlags), true)) {
$options = $specialHandlers[self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY]; $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY];
$handler = $this->setLogItemsOnErrorOnly( $handler = $this->setLogItemsOnErrorOnly(
$handler, $handler,
/** @phpstan-ignore argument.type */ /** @phpstan-ignore argument.type */
@ -170,11 +190,11 @@ class LoggerFactory
); );
} }
if (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, $specialHandlers, true)) { if (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, $specialHandlerFlags, true)) {
/** @phpstan-ignore argument.type */ /** @phpstan-ignore argument.type */
$handler = $this->makeUnique($handler); $handler = $this->makeUnique($handler);
} elseif (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, array_keys($specialHandlers), true)) { } elseif (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, array_keys($specialHandlerFlags), true)) {
$options = $specialHandlers[self::SPECIAL_HANDLERS_MAKE_UNIQUE]; $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_MAKE_UNIQUE];
$handler = $this->makeUnique( $handler = $this->makeUnique(
/** @phpstan-ignore argument.type */ /** @phpstan-ignore argument.type */
$handler, $handler,
@ -216,4 +236,24 @@ class LoggerFactory
/** @phpstan-ignore argument.type */ /** @phpstan-ignore argument.type */
return new DeduplicationHandler($handler, null, $deduplicationLevel, $time); return new DeduplicationHandler($handler, null, $deduplicationLevel, $time);
} }
public function applyProcessors($logger, array $processorFlags): Logger
{
if (in_array(self::PROCESSOR_UNIQUE_ID, $processorFlags, true)) {
$logger->pushProcessor(new UidProcessor());
}
if (in_array(self::PROCESSOR_FILTERSENSITIVE, array_keys($processorFlags), true)) {
$options = $processorFlags[self::PROCESSOR_FILTERSENSITIVE] ?? [];
$searchList = $options[self::FILTERSENSITIVE_SECRETS] ?? [];
if (!is_array($searchList)) {
throw new RuntimeException('sensitive list must be an array');
}
$logger->pushProcessor(new SensitiveFilterProcessor($searchList));
}
return $logger;
}
} }