loggerFactory/src/LoggerFactory.php

259 Zeilen
8.8 KiB
PHP

<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* 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\LoggerFactory;
use Exception;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\DeduplicationHandler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossedHandler;
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;
class LoggerFactory
{
public const SPECIAL_HANDLERS_BUFFERING = 'buffering';
public const SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY = 'logOnErrorOnly';
public const SPECIAL_HANDLERS_MAKE_UNIQUE = 'makeUnique';
public const BUFFERING_OPTION_LIMIT = 'bufferLimit';
public const BUFFERING_OPTION_LEVEL = 'loglevel';
public const LOGONERRORONLY_LEVEL = 'activationLevel';
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();
}
/**
* @param string $loggerName
* @param string $filePath
* @param int $logLevel
* @param int|null $maxFiles
* @param array<int|string, string|array<string, string>> $specialHandlerFlags
* @param array<int|string, string|array<string, string>> $processorFlags
* @return Logger
* @throws Exception
*/
public function getFileLogger(
string $loggerName,
string $filePath,
int $logLevel = Logger::INFO,
?int $maxFiles = null,
array $specialHandlerFlags = [],
array $processorFlags = []
): Logger {
$logger = new Logger($loggerName);
$handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlerFlags
);
$logger->pushHandler($handler);
$this->applyProcessors(
$logger,
$processorFlags
);
return $logger;
}
/**
* @throws Exception
*/
public function getFileLoggerStreamHandler(
string $filePath,
int $logLevel = Logger::INFO,
?int $maxFiles = null
): AbstractProcessingHandler {
return is_null($maxFiles) ?
/** @phpstan-ignore argument.type */
new StreamHandler($filePath, $logLevel) :
/** @phpstan-ignore argument.type */
new RotatingFileHandler($filePath, $maxFiles, $logLevel);
}
/**
* @param string $loggerName
* @param string $filePath
* @param int $logLevel
* @param int|null $maxFiles
* @param array<int|string, string|array<string, string>> $specialHandlerFlags
* @param array<int|string, string|array<string, string>> $processorFlags
* @return Logger
* @throws Exception
*/
public function getCombinedOxidAndFileLogger(
string $loggerName,
string $filePath,
int $logLevel = Logger::INFO,
?int $maxFiles = null,
array $specialHandlerFlags = [],
array $processorFlags = []
): Logger {
if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
}
$logger = new Logger($loggerName);
$handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlerFlags
);
$logger->pushHandler($handler);
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
$oxidHandler = $this->applySpecialHandlers(
new StreamHandler($oxidLogFilePath, Logger::ERROR),
$specialHandlerFlags
);
$logger->pushHandler($oxidHandler);
$this->applyProcessors(
$logger,
$processorFlags
);
return $logger;
}
public function getOxidLogPath(string $fileName): string
{
if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
}
return OX_BASE_PATH . '/log' . DIRECTORY_SEPARATOR . $fileName;
}
/**
* @param AbstractProcessingHandler $handler
* @param array<int|string, string|array<string, string|int>> $specialHandlerFlags
* @return HandlerInterface
*/
public function applySpecialHandlers(
AbstractProcessingHandler $handler,
array $specialHandlerFlags = []
): HandlerInterface {
if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlerFlags, true)) {
$handler = $this->setBuffering($handler);
} 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 */
$options[self::BUFFERING_OPTION_LIMIT] ?? 0,
/** @phpstan-ignore argument.type */
$options[self::BUFFERING_OPTION_LEVEL] ?? Logger::DEBUG
);
}
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($specialHandlerFlags), true)) {
$options = $specialHandlerFlags[self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY];
$handler = $this->setLogItemsOnErrorOnly(
$handler,
/** @phpstan-ignore argument.type */
$options[self::LOGONERRORONLY_LEVEL] ?? Logger::ERROR
);
}
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($specialHandlerFlags), true)) {
$options = $specialHandlerFlags[self::SPECIAL_HANDLERS_MAKE_UNIQUE];
$handler = $this->makeUnique(
/** @phpstan-ignore argument.type */
$handler,
/** @phpstan-ignore argument.type */
$options[self::MAKEUNIQUE_OPTION_LEVEL] ?? Logger::ERROR,
/** @phpstan-ignore argument.type */
$options[self::MAKEUNIQUE_OPTION_TIME] ?? 60
);
}
return $handler;
}
public function setBuffering(
AbstractHandler $handler,
int $bufferLimit = 0,
int $loglevel = Logger::DEBUG
): BufferHandler {
/** @phpstan-ignore argument.type */
return new BufferHandler($handler, $bufferLimit, $loglevel);
}
public function setLogItemsOnErrorOnly(
AbstractHandler $handler,
int $activationLevel = Logger::ERROR
): FingersCrossedHandler {
return new FingersCrossedHandler(
$handler,
/** @phpstan-ignore argument.type */
new ErrorLevelActivationStrategy($activationLevel)
);
}
public function makeUnique(
AbstractHandler $handler,
int $deduplicationLevel = Logger::ERROR,
int $time = 60
): DeduplicationHandler {
/** @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;
}
}