Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 29 |
| ExceptionLoggingSubscriber | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 29 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| on403 | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| on404 | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| onError | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 8 |
|||
| onException | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 10 |
|||
| getSubscribedEvents | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber. | |
| */ | |
| namespace Drupal\Core\EventSubscriber; | |
| use Drupal\Core\Logger\LoggerChannelFactoryInterface; | |
| use Drupal\Core\Utility\Error; | |
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
| use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
| use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | |
| use Symfony\Component\HttpKernel\KernelEvents; | |
| /** | |
| * Log exceptions without further handling. | |
| */ | |
| 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\GetResponseForExceptionEvent $event | |
| * The event to process. | |
| */ | |
| public function on403(GetResponseForExceptionEvent $event) { | |
| $request = $event->getRequest(); | |
| $this->logger->get('access denied')->warning('@uri', ['@uri' => $request->getRequestUri()]); | |
| } | |
| /** | |
| * Log 404 errors. | |
| * | |
| * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
| * The event to process. | |
| */ | |
| public function on404(GetResponseForExceptionEvent $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\GetResponseForExceptionEvent $event | |
| * The event to process. | |
| */ | |
| public function onError(GetResponseForExceptionEvent $event) { | |
| $exception = $event->getException(); | |
| $error = Error::decodeException($exception); | |
| $this->logger->get('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $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 all exceptions. | |
| * | |
| * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
| * The event to process. | |
| */ | |
| public function onException(GetResponseForExceptionEvent $event) { | |
| $exception = $event->getException(); | |
| $method = 'onError'; | |
| // Treat any non-HTTP exception as if it were one, so we log it the same. | |
| if ($exception instanceof HttpExceptionInterface) { | |
| $possible_method = 'on' . $exception->getStatusCode(); | |
| if (method_exists($this, $possible_method)) { | |
| $method = $possible_method; | |
| } | |
| } | |
| $this->$method($event); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function getSubscribedEvents() { | |
| $events[KernelEvents::EXCEPTION][] = ['onException', 50]; | |
| return $events; | |
| } | |
| } |