oxid-sql-logger/src/LoggerFactory.php

132 regels
3.7 KiB
PHP

<?php
2019-09-19 19:52:54 +02:00
/**
2019-09-19 19:52:54 +02:00
* @author Tobias Matthaiou <developer@tobimat.eu>
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
*/
2019-09-19 19:52:54 +02:00
2024-02-02 10:59:40 +01:00
declare(strict_types=1);
2019-09-19 19:52:54 +02:00
namespace D3\OxidSqlLogger;
use Monolog;
2024-02-02 11:33:11 +01:00
use Monolog\Logger;
2020-05-13 18:53:56 +02:00
use OxidEsales\Eshop\Core\Registry;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class LoggerFactory
{
/**
2024-02-02 11:33:11 +01:00
* @param string $name
*
* @return Logger
*/
2024-02-02 11:33:11 +01:00
public function create(string $name): Monolog\Logger
{
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
}
/**
* @return array
*/
2024-02-02 10:59:40 +01:00
private function getHandlers(): array
{
if (PHP_SAPI == 'cli') {
2020-05-13 18:53:56 +02:00
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers');
2024-02-02 10:59:40 +01:00
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
2020-05-13 18:53:56 +02:00
$this->getInstancesFromHandlerList($configuredHandlers) :
2021-03-02 15:50:39 +01:00
[
2024-02-02 11:39:13 +01:00
$this->getStreamHandler(),
2021-03-02 15:50:39 +01:00
];
} else {
2020-05-13 18:53:56 +02:00
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
2024-02-02 10:59:40 +01:00
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
2020-05-13 18:53:56 +02:00
$this->getInstancesFromHandlerList($configuredHandlers) :
[
$this->getBrowserConsoleHandler(),
2024-02-02 11:39:13 +01:00
$this->getFirePHPHandler(),
2020-05-13 18:53:56 +02:00
];
}
2020-05-13 18:53:56 +02:00
return $handlers;
}
2020-05-13 18:53:56 +02:00
/**
2024-02-02 11:33:11 +01:00
* @param iterable $classNames
2020-05-13 18:53:56 +02:00
*
* @return array
*/
2024-02-02 11:33:11 +01:00
private function getInstancesFromHandlerList(iterable $classNames): array
2020-05-13 18:53:56 +02:00
{
return array_map(
2024-02-02 11:39:13 +01:00
function ($className) {
2020-05-13 18:53:56 +02:00
return new $className();
},
2024-02-02 11:33:11 +01:00
(array) $classNames
2020-05-13 18:53:56 +02:00
);
}
/**
* @return Monolog\Handler\StreamHandler
*/
2024-02-02 10:59:40 +01:00
private function getStreamHandler(): Monolog\Handler\StreamHandler
{
$streamHandler = new Monolog\Handler\StreamHandler('php://stderr');
$channel = (new OutputFormatterStyle(null, null, ['bold']))->apply('%channel%');
$level_name = (new OutputFormatterStyle('blue'))->apply('%level_name%');
$message = (new OutputFormatterStyle('green'))->apply('%message%');
$context = (new OutputFormatterStyle('yellow'))->apply('%context%');
$newline = PHP_EOL . str_repeat(' ', 10);
2022-08-18 14:18:43 +02:00
$ttl_color = "$channel $level_name: $message $newline $context $newline %extra%" . PHP_EOL;
$streamHandler->setFormatter(
new Monolog\Formatter\LineFormatter(
$ttl_color,
null,
true,
true
)
);
return $streamHandler;
}
/**
* @return Monolog\Handler\BrowserConsoleHandler
*/
2024-02-02 10:59:40 +01:00
private function getBrowserConsoleHandler(): Monolog\Handler\BrowserConsoleHandler
{
return new Monolog\Handler\BrowserConsoleHandler();
}
/**
* @return Monolog\Handler\FirePHPHandler
*/
2024-02-02 10:59:40 +01:00
private function getFirePHPHandler(): Monolog\Handler\FirePHPHandler
{
return new Monolog\Handler\FirePHPHandler();
}
/**
* @return array
*/
2024-02-02 10:59:40 +01:00
private function getProcessors(): array
{
return [
2024-02-02 10:59:40 +01:00
new Monolog\Processor\IntrospectionProcessor(
Monolog\Logger::DEBUG,
[
'D3\\OxidSqlLogger',
'Doctrine\\DBAL\\Connection',
2024-02-02 11:39:13 +01:00
'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database',
2024-02-02 10:59:40 +01:00
]
),
new Monolog\Processor\PsrLogMessageProcessor(),
];
}
}