PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/longphu/drupal8
PHP | 113 lines | 47 code | 16 blank | 50 comment | 4 complexity | 24cb3d8673b47d522b56aacc7dae68d6 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\Component\Utility\String;
  8. use Drupal\Core\Logger\LoggerChannelFactoryInterface;
  9. use Drupal\Core\Utility\Error;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  12. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15. * Log exceptions without further handling.
  16. */
  17. class ExceptionLoggingSubscriber implements EventSubscriberInterface {
  18. /**
  19. * The logger channel factory.
  20. *
  21. * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
  22. */
  23. protected $logger;
  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. * Log 403 errors.
  35. *
  36. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  37. * The event to process.
  38. */
  39. public function on403(GetResponseForExceptionEvent $event) {
  40. $request = $event->getRequest();
  41. // @todo Remove dependency on the internal _system_path attribute:
  42. // https://www.drupal.org/node/2293523.
  43. $this->logger->get('access denied')->warning(String::checkPlain($request->attributes->get('_system_path')));
  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. // @todo Remove dependency on the internal _system_path attribute:
  54. // https://www.drupal.org/node/2293523.
  55. $this->logger->get('page not found')->warning(String::checkPlain($request->attributes->get('_system_path')));
  56. }
  57. /**
  58. * Log not-otherwise-specified errors, including HTTP 500.
  59. *
  60. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  61. * The event to process.
  62. */
  63. public function onError(GetResponseForExceptionEvent $event) {
  64. $exception = $event->getException();
  65. $error = Error::decodeException($exception);
  66. $this->logger->get('php')->log($error['severity_level'], '%type: !message in %function (line %line of %file).', $error);
  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. * Log all exceptions.
  74. *
  75. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  76. * The event to process.
  77. */
  78. public function onException(GetResponseForExceptionEvent $event) {
  79. $exception = $event->getException();
  80. $method = 'onError';
  81. // Treat any non-HTTP exception as if it were one, so we log it the same.
  82. if ($exception instanceof HttpExceptionInterface) {
  83. $possible_method = 'on' . $exception->getStatusCode();
  84. if (method_exists($this, $possible_method)) {
  85. $method = $possible_method;
  86. }
  87. }
  88. $this->$method($event);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public static function getSubscribedEvents() {
  94. $events[KernelEvents::EXCEPTION][] = ['onException', 50];
  95. return $events;
  96. }
  97. }