113 lines
3.1 KiB
PHP
113 lines
3.1 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\AbstractProcessingHandler;
|
||
|
use Monolog\Handler\RotatingFileHandler;
|
||
|
use Monolog\Handler\StreamHandler;
|
||
|
use Monolog\Logger;
|
||
|
use OxidEsales\Eshop\Core\Registry;
|
||
|
use RuntimeException;
|
||
|
|
||
|
class LoggerFactory
|
||
|
{
|
||
|
public static function create(): LoggerFactory
|
||
|
{
|
||
|
return new LoggerFactory();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $loggerName
|
||
|
* @param string $filePath
|
||
|
* @param int $logLevel
|
||
|
* @param int|null $maxFiles
|
||
|
* @return Logger
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public function getFileLogger(
|
||
|
string $loggerName,
|
||
|
string $filePath,
|
||
|
int $logLevel = Logger::INFO,
|
||
|
?int $maxFiles = null
|
||
|
): Logger
|
||
|
{
|
||
|
$logger = new Logger($loggerName);
|
||
|
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||
|
$logger->pushHandler($stream_handler);
|
||
|
|
||
|
return $logger;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $filePath
|
||
|
* @param int $logLevel
|
||
|
* @param int|null $maxFiles
|
||
|
* @return AbstractProcessingHandler
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
protected function getFileLoggerStreamHandler(
|
||
|
string $filePath,
|
||
|
int $logLevel = Logger::INFO,
|
||
|
?int $maxFiles = null
|
||
|
): AbstractProcessingHandler
|
||
|
{
|
||
|
return is_null($maxFiles) ?
|
||
|
new StreamHandler($filePath, $logLevel) :
|
||
|
new RotatingFileHandler($filePath, $maxFiles, $logLevel);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $loggerName
|
||
|
* @param string $filePath
|
||
|
* @param int $logLevel
|
||
|
* @param int|null $maxFiles
|
||
|
* @return Logger
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public function getCombinedOxidAndFileLogger(
|
||
|
string $loggerName,
|
||
|
string $filePath,
|
||
|
int $logLevel = Logger::INFO,
|
||
|
?int $maxFiles = null
|
||
|
): Logger
|
||
|
{
|
||
|
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);
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|