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