This commit is contained in:
2026-04-09 22:51:22 +02:00
parent e17b7026fd
commit a9221e3f5d
2 changed files with 70 additions and 51 deletions

View File

@@ -12,18 +12,62 @@
namespace JchOptimize\Log;
use Joomla\CMS\Log\DelegatingPsrLogger;
use Joomla\CMS\Log\Log;
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
use Stringable;
\defined('_JEXEC') or exit('Restricted Access');
/**
* @psalm-suppress all
*/
class DelegatingPsrLoggerExtended extends DelegatingPsrLogger
class DelegatingPsrLoggerExtended extends AbstractLogger
{
public function log($level, $message, array $context = [])
/**
* @var Log
*/
protected $logger;
/**
* @var array<string, int>
*/
protected $priorityMap = [
LogLevel::EMERGENCY => Log::EMERGENCY,
LogLevel::ALERT => Log::ALERT,
LogLevel::CRITICAL => Log::CRITICAL,
LogLevel::ERROR => Log::ERROR,
LogLevel::WARNING => Log::WARNING,
LogLevel::NOTICE => Log::NOTICE,
LogLevel::INFO => Log::INFO,
LogLevel::DEBUG => Log::DEBUG,
];
public function __construct(Log $logger)
{
$context = \array_merge($context, ['category' => 'com_jchoptimize']);
parent::log($level, $message, $context);
$this->logger = $logger;
}
public function log($level, string|Stringable $message, array $context = []): void
{
if (!\array_key_exists($level, $this->priorityMap)) {
throw new \InvalidArgumentException('An invalid log level has been given.');
}
$priority = $this->priorityMap[$level];
$context = \array_merge($context, ['category' => 'com_jchoptimize']);
$category = null;
$date = null;
if (!empty($context['category'])) {
$category = $context['category'];
}
if (!empty($context['date'])) {
$date = $context['date'];
}
$this->logger::add((string) $message, $priority, $category, $date, $context);
}
}