<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Psr\Log\LoggerInterface;
class ExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var LoggerInterface;
*/
private $logger;
public function __construct(
LoggerInterface $maxxtonLogger
) {
$this->logger = $maxxtonLogger;
}
public function onKernelException(ExceptionEvent $event)
{
// ...
$exception = $event->getThrowable();
if($exception instanceof HttpException){
$this->logger->error($exception->getMessage(), [
'statusCode' => $exception->getStatusCode(),
]);
}
}
public static function getSubscribedEvents()
{
return [
'kernel.exception' => 'onKernelException',
];
}
}