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