can add special handlers
This commit is contained in:
parent
e46b8bcd0f
commit
9adef0b900
@ -19,6 +19,9 @@ namespace D3\LoggerFactory;
|
||||
|
||||
use Exception;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Handler\BufferHandler;
|
||||
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
|
||||
use Monolog\Handler\FingersCrossedHandler;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
@ -27,41 +30,39 @@ use RuntimeException;
|
||||
|
||||
class LoggerFactory
|
||||
{
|
||||
public const SPECIAL_HANDLERS_BUFFERING = 'buffering';
|
||||
public const SPECIAL_HANDLERS_LOG_ON_ERROR_ONLY = 'logOnErrorOnly';
|
||||
|
||||
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
|
||||
?int $maxFiles = null,
|
||||
array $specialHandlers = []
|
||||
): Logger
|
||||
{
|
||||
$logger = new Logger($loggerName);
|
||||
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||||
$logger->pushHandler($stream_handler);
|
||||
$handler = $this->applySpecialHandlers(
|
||||
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
|
||||
$specialHandlers
|
||||
);
|
||||
$logger->pushHandler($handler);
|
||||
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filePath
|
||||
* @param int $logLevel
|
||||
* @param int|null $maxFiles
|
||||
* @return AbstractProcessingHandler
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getFileLoggerStreamHandler(
|
||||
public function getFileLoggerStreamHandler(
|
||||
string $filePath,
|
||||
int $logLevel = Logger::INFO,
|
||||
?int $maxFiles = null
|
||||
@ -73,18 +74,14 @@ class LoggerFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
?int $maxFiles = null,
|
||||
array $specialHandlers = []
|
||||
): Logger
|
||||
{
|
||||
if (!class_exists(Registry::class)) {
|
||||
@ -92,12 +89,18 @@ class LoggerFactory
|
||||
}
|
||||
|
||||
$logger = new Logger($loggerName);
|
||||
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||||
$logger->pushHandler($stream_handler);
|
||||
$handler = $this->applySpecialHandlers(
|
||||
$this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles),
|
||||
$specialHandlers
|
||||
);
|
||||
$logger->pushHandler($handler);
|
||||
|
||||
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
|
||||
$oxidStreamHandler = new StreamHandler($oxidLogFilePath, Logger::ERROR);
|
||||
$logger->pushHandler($oxidStreamHandler);
|
||||
$oxidHandler = $this->applySpecialHandlers(
|
||||
new StreamHandler($oxidLogFilePath, Logger::ERROR),
|
||||
$specialHandlers
|
||||
);
|
||||
$logger->pushHandler($oxidHandler);
|
||||
|
||||
return $logger;
|
||||
}
|
||||
@ -110,4 +113,35 @@ class LoggerFactory
|
||||
|
||||
return OX_BASE_PATH . '/log' . DIRECTORY_SEPARATOR . $fileName;
|
||||
}
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user