diff --git a/src/LoggerFactory.php b/src/LoggerFactory.php index ed4a1b6..e2eb599 100644 --- a/src/LoggerFactory.php +++ b/src/LoggerFactory.php @@ -28,6 +28,7 @@ use Monolog\Handler\HandlerInterface; use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\StreamHandler; use Monolog\Logger; +use Monolog\Processor\UidProcessor; use OxidEsales\Eshop\Core\Registry; use RuntimeException; @@ -45,6 +46,11 @@ class LoggerFactory public const MAKEUNIQUE_OPTION_LEVEL = 'loglevel'; 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 { return new LoggerFactory(); @@ -55,7 +61,8 @@ class LoggerFactory * @param string $filePath * @param int $logLevel * @param int|null $maxFiles - * @param array> $specialHandlers + * @param array> $specialHandlerFlags + * @param array> $processorFlags * @return Logger * @throws Exception */ @@ -64,15 +71,21 @@ class LoggerFactory string $filePath, int $logLevel = Logger::INFO, ?int $maxFiles = null, - array $specialHandlers = [] + array $specialHandlerFlags = [], + array $processorFlags = [] ): Logger { $logger = new Logger($loggerName); $handler = $this->applySpecialHandlers( $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles), - $specialHandlers + $specialHandlerFlags ); $logger->pushHandler($handler); + $this->applyProcessors( + $logger, + $processorFlags + ); + return $logger; } @@ -96,7 +109,8 @@ class LoggerFactory * @param string $filePath * @param int $logLevel * @param int|null $maxFiles - * @param array> $specialHandlers + * @param array> $specialHandlerFlags + * @param array> $processorFlags * @return Logger * @throws Exception */ @@ -105,7 +119,8 @@ class LoggerFactory string $filePath, int $logLevel = Logger::INFO, ?int $maxFiles = null, - array $specialHandlers = [] + array $specialHandlerFlags = [], + array $processorFlags = [] ): Logger { if (!class_exists(Registry::class)) { throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); @@ -114,21 +129,26 @@ class LoggerFactory $logger = new Logger($loggerName); $handler = $this->applySpecialHandlers( $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles), - $specialHandlers + $specialHandlerFlags ); $logger->pushHandler($handler); $oxidLogFilePath = $this->getOxidLogPath('oxideshop.log'); $oxidHandler = $this->applySpecialHandlers( new StreamHandler($oxidLogFilePath, Logger::ERROR), - $specialHandlers + $specialHandlerFlags ); $logger->pushHandler($oxidHandler); + $this->applyProcessors( + $logger, + $processorFlags + ); + return $logger; } - protected function getOxidLogPath(string $fileName): string + public function getOxidLogPath(string $fileName): string { if (!class_exists(Registry::class)) { throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only'); @@ -139,17 +159,17 @@ class LoggerFactory /** * @param AbstractProcessingHandler $handler - * @param array> $specialHandlers + * @param array> $specialHandlerFlags * @return HandlerInterface */ public function applySpecialHandlers( AbstractProcessingHandler $handler, - array $specialHandlers = [] + array $specialHandlerFlags = [] ): HandlerInterface { - if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlers, true)) { + if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlerFlags, true)) { $handler = $this->setBuffering($handler); - } elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlers), true)) { - $options = $specialHandlers[self::SPECIAL_HANDLERS_BUFFERING]; + } elseif (in_array(self::SPECIAL_HANDLERS_BUFFERING, array_keys($specialHandlerFlags), true)) { + $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_BUFFERING]; $handler = $this->setBuffering( $handler, /** @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); - } elseif (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, array_keys($specialHandlers), true)) { - $options = $specialHandlers[self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY]; + } elseif (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, array_keys($specialHandlerFlags), true)) { + $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY]; $handler = $this->setLogItemsOnErrorOnly( $handler, /** @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 */ $handler = $this->makeUnique($handler); - } elseif (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, array_keys($specialHandlers), true)) { - $options = $specialHandlers[self::SPECIAL_HANDLERS_MAKE_UNIQUE]; + } elseif (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, array_keys($specialHandlerFlags), true)) { + $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_MAKE_UNIQUE]; $handler = $this->makeUnique( /** @phpstan-ignore argument.type */ $handler, @@ -216,4 +236,24 @@ class LoggerFactory /** @phpstan-ignore argument.type */ 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; + } +} \ No newline at end of file