<?php declare(strict_types=1);
namespace TigerMedia\Base\Subscriber;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use TigerMedia\Base\Service\ConfigurationService;
class LogKernelExceptions implements EventSubscriberInterface
{
private LoggerInterface $logger;
private ConfigurationService $configurationService;
public function __construct(ConfigurationService $configurationService, LoggerInterface $logger)
{
$this->logger = $logger;
$this->configurationService = $configurationService;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException'
];
}
public function onKernelException(ExceptionEvent $event): void
{
if (!$this->configurationService->isErrorLogEnabled()) {
return;
}
$this->logger->error($event->getThrowable()->getMessage(), [
'exception' => $event->getThrowable(),
]);
}
}