PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php

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