Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\EventSubscriber; | |
4 | |
5 use Drupal\Core\Logger\LoggerChannelFactoryInterface; | |
6 use Drupal\Core\Utility\Error; | |
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
8 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
9 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | |
10 use Symfony\Component\HttpKernel\KernelEvents; | |
11 | |
12 /** | |
13 * Log exceptions without further handling. | |
14 */ | |
15 class ExceptionLoggingSubscriber implements EventSubscriberInterface { | |
16 | |
17 /** | |
18 * The logger channel factory. | |
19 * | |
20 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface | |
21 */ | |
22 protected $logger; | |
23 | |
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 /** | |
35 * Log 403 errors. | |
36 * | |
37 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
38 * The event to process. | |
39 */ | |
40 public function on403(GetResponseForExceptionEvent $event) { | |
41 $request = $event->getRequest(); | |
42 $this->logger->get('access denied')->warning('@uri', ['@uri' => $request->getRequestUri()]); | |
43 } | |
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 $this->logger->get('page not found')->warning('@uri', ['@uri' => $request->getRequestUri()]); | |
54 } | |
55 | |
56 /** | |
57 * Log not-otherwise-specified errors, including HTTP 500. | |
58 * | |
59 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
60 * The event to process. | |
61 */ | |
62 public function onError(GetResponseForExceptionEvent $event) { | |
63 $exception = $event->getException(); | |
64 $error = Error::decodeException($exception); | |
65 $this->logger->get('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error); | |
66 | |
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 /** | |
74 * Log all exceptions. | |
75 * | |
76 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
77 * The event to process. | |
78 */ | |
79 public function onException(GetResponseForExceptionEvent $event) { | |
80 $exception = $event->getException(); | |
81 | |
82 $method = 'onError'; | |
83 | |
84 // Treat any non-HTTP exception as if it were one, so we log it the same. | |
85 if ($exception instanceof HttpExceptionInterface) { | |
86 $possible_method = 'on' . $exception->getStatusCode(); | |
87 if (method_exists($this, $possible_method)) { | |
88 $method = $possible_method; | |
89 } | |
90 } | |
91 | |
92 $this->$method($event); | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public static function getSubscribedEvents() { | |
99 $events[KernelEvents::EXCEPTION][] = ['onException', 50]; | |
100 return $events; | |
101 } | |
102 | |
103 } |