class ExceptionLoggingSubscriber

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber
  3. 11.x core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber

Log exceptions without further handling.

Hierarchy

  • class \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ExceptionLoggingSubscriber

1 string reference to 'ExceptionLoggingSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ExceptionLoggingSubscriber
exception.logger in core/core.services.yml
Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php, line 15

Namespace

Drupal\Core\EventSubscriber
View source
class ExceptionLoggingSubscriber implements EventSubscriberInterface {
    
    /**
     * The logger channel factory.
     *
     * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
     */
    protected $logger;
    
    /**
     * Constructs a new ExceptionLoggingSubscriber.
     *
     * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
     *   The logger channel factory.
     */
    public function __construct(LoggerChannelFactoryInterface $logger) {
        $this->logger = $logger;
    }
    
    /**
     * Log 403 errors.
     *
     * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
     *   The event to process.
     */
    public function on403(ExceptionEvent $event) {
        // Log the exception with the page where it happened so that admins know
        // why access was denied.
        $exception = $event->getThrowable();
        $error = Error::decodeException($exception);
        unset($error['@backtrace_string']);
        $error['@uri'] = $event->getRequest()
            ->getRequestUri();
        $this->logger
            ->get('access denied')
            ->warning('Path: @uri. ' . Error::DEFAULT_ERROR_MESSAGE, $error);
    }
    
    /**
     * Log 404 errors.
     *
     * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
     *   The event to process.
     */
    public function on404(ExceptionEvent $event) {
        $request = $event->getRequest();
        $this->logger
            ->get('page not found')
            ->warning('@uri', [
            '@uri' => $request->getRequestUri(),
        ]);
    }
    
    /**
     * Log not-otherwise-specified errors, including HTTP 500.
     *
     * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
     *   The event to process.
     */
    public function onError(ExceptionEvent $event) {
        $exception = $event->getThrowable();
        $error = Error::decodeException($exception);
        $this->logger
            ->get('php')
            ->log($error['severity_level'], Error::DEFAULT_ERROR_MESSAGE, $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()));
        }
    }
    
    /**
     * Log 4xx errors that are not 403 or 404.
     *
     * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
     *   The event to process.
     */
    public function onClientError(ExceptionEvent $event) {
        $exception = $event->getThrowable();
        $error = Error::decodeException($exception);
        $error += [
            'status_code' => $exception->getStatusCode(),
        ];
        unset($error['@backtrace_string']);
        $this->logger
            ->get('client error')
            ->warning(Error::DEFAULT_ERROR_MESSAGE, $error);
    }
    
    /**
     * Log all exceptions.
     *
     * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
     *   The event to process.
     */
    public function onException(ExceptionEvent $event) {
        $exception = $event->getThrowable();
        $method = 'onError';
        // Treat any non-HTTP exception as if it were one, so we log it the same.
        if ($exception instanceof HttpExceptionInterface) {
            $status_code = $exception->getStatusCode();
            $possible_method = 'on' . $status_code;
            if (method_exists($this, $possible_method)) {
                $method = $possible_method;
            }
            elseif ($status_code >= 400 && $status_code < 500) {
                $method = 'onClientError';
            }
        }
        $this->{$method}($event);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[KernelEvents::EXCEPTION][] = [
            'onException',
            50,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ExceptionLoggingSubscriber::$logger protected property The logger channel factory.
ExceptionLoggingSubscriber::getSubscribedEvents public static function
ExceptionLoggingSubscriber::on403 public function Log 403 errors.
ExceptionLoggingSubscriber::on404 public function Log 404 errors.
ExceptionLoggingSubscriber::onClientError public function Log 4xx errors that are not 403 or 404.
ExceptionLoggingSubscriber::onError public function Log not-otherwise-specified errors, including HTTP 500.
ExceptionLoggingSubscriber::onException public function Log all exceptions.
ExceptionLoggingSubscriber::__construct public function Constructs a new ExceptionLoggingSubscriber.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.