loggerFactory/src/LoggerFactory.php

147 Zeilen
4.2 KiB
PHP

2025-02-05 21:57:47 +01:00
<?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\AbstractProcessingHandler;
2025-02-06 22:51:32 +01:00
use Monolog\Handler\BufferHandler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossedHandler;
2025-02-05 21:57:47 +01:00
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OxidEsales\Eshop\Core\Registry;
use RuntimeException;
class LoggerFactory
{
2025-02-06 22:51:32 +01:00
public const SPECIAL_HANDLERS_BUFFERING = 'buffering';
public const SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY = 'logOnErrorOnly';
2025-02-05 21:57:47 +01:00
public static function create(): LoggerFactory
{
return new LoggerFactory();
}
/**
* @throws Exception
*/
public function getFileLogger(
string $loggerName,
string $filePath,
int $logLevel = Logger::INFO,
2025-02-06 22:51:32 +01:00
?int $maxFiles = null,
array $specialHandlers = []
2025-02-05 21:57:47 +01:00
): Logger
{
$logger = new Logger($loggerName);
2025-02-06 22:51:32 +01:00
$handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlers
);
$logger->pushHandler($handler);
2025-02-05 21:57:47 +01:00
return $logger;
}
/**
* @throws Exception
*/
2025-02-06 22:51:32 +01:00
public function getFileLoggerStreamHandler(
2025-02-05 21:57:47 +01:00
string $filePath,
int $logLevel = Logger::INFO,
?int $maxFiles = null
): AbstractProcessingHandler
{
return is_null($maxFiles) ?
new StreamHandler($filePath, $logLevel) :
new RotatingFileHandler($filePath, $maxFiles, $logLevel);
}
/**
* @throws Exception
*/
public function getCombinedOxidAndFileLogger(
string $loggerName,
string $filePath,
int $logLevel = Logger::INFO,
2025-02-06 22:51:32 +01:00
?int $maxFiles = null,
array $specialHandlers = []
2025-02-05 21:57:47 +01:00
): Logger
{
if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
}
$logger = new Logger($loggerName);
2025-02-06 22:51:32 +01:00
$handler = $this->applySpecialHandlers(
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
$specialHandlers
);
$logger->pushHandler($handler);
2025-02-05 21:57:47 +01:00
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
2025-02-06 22:51:32 +01:00
$oxidHandler = $this->applySpecialHandlers(
new StreamHandler($oxidLogFilePath, Logger::ERROR),
$specialHandlers
);
$logger->pushHandler($oxidHandler);
2025-02-05 21:57:47 +01:00
return $logger;
}
protected 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;
}
2025-02-06 22:51:32 +01:00
public function applySpecialHandlers(
AbstractProcessingHandler $handler,
array $specialHandlers = []
): AbstractProcessingHandler
{
if (in_array(self::SPECIAL_HANDLERS_BUFFERING, $specialHandlers, true)) {
$handler = $this->setLogItemsOnErrorOnly($handler);
}
if (in_array(self::SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY, $specialHandlers, true)) {
$handler = $this->setLogItemsOnErrorOnly($handler);
}
return $handler;
}
public function setBuffering(AbstractProcessingHandler $handler): BufferHandler
{
return new BufferHandler($handler);
}
public function setLogItemsOnErrorOnly(
AbstractProcessingHandler $handler,
int $activationLevel = Logger::ERROR
): FingersCrossedHandler
{
return new FingersCrossedHandler(
$handler,
new ErrorLevelActivationStrategy($activationLevel)
);
}
2025-02-05 21:57:47 +01:00
}