ExceptionLoggingSubscriber.php in Zircon Profile 8.0
File
core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Utility\Error;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionLoggingSubscriber implements EventSubscriberInterface {
protected $logger;
public function __construct(LoggerChannelFactoryInterface $logger) {
$this->logger = $logger;
}
public function on403(GetResponseForExceptionEvent $event) {
$request = $event
->getRequest();
$this->logger
->get('access denied')
->warning('@uri', [
'@uri' => $request
->getRequestUri(),
]);
}
public function on404(GetResponseForExceptionEvent $event) {
$request = $event
->getRequest();
$this->logger
->get('page not found')
->warning('@uri', [
'@uri' => $request
->getRequestUri(),
]);
}
public function onError(GetResponseForExceptionEvent $event) {
$exception = $event
->getException();
$error = Error::decodeException($exception);
$this->logger
->get('php')
->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
$is_critical = !$exception instanceof HttpExceptionInterface || $exception
->getStatusCode() >= 500;
if ($is_critical) {
error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception
->getMessage(), $exception
->getFile(), $exception
->getLine()));
}
}
public function onException(GetResponseForExceptionEvent $event) {
$exception = $event
->getException();
$method = 'onError';
if ($exception instanceof HttpExceptionInterface) {
$possible_method = 'on' . $exception
->getStatusCode();
if (method_exists($this, $possible_method)) {
$method = $possible_method;
}
}
$this
->{$method}($event);
}
public static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = [
'onException',
50,
];
return $events;
}
}