annotate core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
Chris@0 6 use Drupal\Core\Utility\Error;
Chris@0 7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 8 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 9 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
Chris@0 10 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Log exceptions without further handling.
Chris@0 14 */
Chris@0 15 class ExceptionLoggingSubscriber implements EventSubscriberInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The logger channel factory.
Chris@0 19 *
Chris@0 20 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
Chris@0 21 */
Chris@0 22 protected $logger;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Constructs a new ExceptionLoggingSubscriber.
Chris@0 26 *
Chris@0 27 * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
Chris@0 28 * The logger channel factory.
Chris@0 29 */
Chris@0 30 public function __construct(LoggerChannelFactoryInterface $logger) {
Chris@0 31 $this->logger = $logger;
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Log 403 errors.
Chris@0 36 *
Chris@0 37 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 38 * The event to process.
Chris@0 39 */
Chris@0 40 public function on403(GetResponseForExceptionEvent $event) {
Chris@0 41 $request = $event->getRequest();
Chris@0 42 $this->logger->get('access denied')->warning('@uri', ['@uri' => $request->getRequestUri()]);
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Log 404 errors.
Chris@0 47 *
Chris@0 48 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 49 * The event to process.
Chris@0 50 */
Chris@0 51 public function on404(GetResponseForExceptionEvent $event) {
Chris@0 52 $request = $event->getRequest();
Chris@0 53 $this->logger->get('page not found')->warning('@uri', ['@uri' => $request->getRequestUri()]);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Log not-otherwise-specified errors, including HTTP 500.
Chris@0 58 *
Chris@0 59 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 60 * The event to process.
Chris@0 61 */
Chris@0 62 public function onError(GetResponseForExceptionEvent $event) {
Chris@0 63 $exception = $event->getException();
Chris@0 64 $error = Error::decodeException($exception);
Chris@0 65 $this->logger->get('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
Chris@0 66
Chris@0 67 $is_critical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
Chris@0 68 if ($is_critical) {
Chris@0 69 error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
Chris@0 70 }
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Log all exceptions.
Chris@0 75 *
Chris@0 76 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 77 * The event to process.
Chris@0 78 */
Chris@0 79 public function onException(GetResponseForExceptionEvent $event) {
Chris@0 80 $exception = $event->getException();
Chris@0 81
Chris@0 82 $method = 'onError';
Chris@0 83
Chris@0 84 // Treat any non-HTTP exception as if it were one, so we log it the same.
Chris@0 85 if ($exception instanceof HttpExceptionInterface) {
Chris@0 86 $possible_method = 'on' . $exception->getStatusCode();
Chris@0 87 if (method_exists($this, $possible_method)) {
Chris@0 88 $method = $possible_method;
Chris@0 89 }
Chris@0 90 }
Chris@0 91
Chris@0 92 $this->$method($event);
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public static function getSubscribedEvents() {
Chris@0 99 $events[KernelEvents::EXCEPTION][] = ['onException', 50];
Chris@0 100 return $events;
Chris@0 101 }
Chris@0 102
Chris@0 103 }