PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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