add combined (OXID + custom) logger

This commit is contained in:
2025-01-26 22:17:24 +01:00
parent 08e9f7c21e
commit 487a5f5b88
7 changed files with 112 additions and 4 deletions

View File

@ -17,6 +17,10 @@ declare(strict_types=1);
namespace D3\GuzzleFactory\Apps;
use Exception;
use InvalidArgumentException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OxidEsales\Eshop\Core\Registry;
use RuntimeException;
@ -31,6 +35,36 @@ trait OxidLoggerTrait
$this->loggers['oxid'] = Registry::getLogger();
}
/**
* @throws Exception
* @throws InvalidArgumentException
*/
public function addCombinedOxidAndFileLogger(
string $loggerName,
string $filePath,
int $logLevel = Logger::INFO,
?int $maxFiles = null
): void
{
if (!class_exists(Registry::class)) {
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
}
$logger = new Logger($loggerName);
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
$logger->pushHandler($stream_handler);
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
$oxidStreamHandler = new StreamHandler($oxidLogFilePath, Logger::ERROR);
$logger->pushHandler($oxidStreamHandler);
if (isset($this->loggers['oxid'])) {
unset($this->loggers['oxid']);
}
$this->loggers[$loggerName] = $logger;
}
public function getOxidLogPath(string $fileName): string
{
if (!class_exists(Registry::class)) {