1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Utility\Error;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
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\ExceptionEvent $event
* The event to process.
*/
public function on403(ExceptionEvent $event) {
// Log the exception with the page where it happened so that admins know
// why access was denied.
$exception = $event->getThrowable();
$error = Error::decodeException($exception);
unset($error['@backtrace_string']);
$error['@uri'] = $event->getRequest()->getRequestUri();
$this->logger->get('access denied')->warning('Path: @uri. ' . Error::DEFAULT_ERROR_MESSAGE, $error);
}
/**
* Log 404 errors.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The event to process.
*/
public function on404(ExceptionEvent $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\ExceptionEvent $event
* The event to process.
*/
public function onError(ExceptionEvent $event) {
$exception = $event->getThrowable();
$error = Error::decodeException($exception);
$this->logger->get('php')->log($error['severity_level'], Error::DEFAULT_ERROR_MESSAGE, $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 4xx errors that are not 403 or 404.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The event to process.
*/
public function onClientError(ExceptionEvent $event) {
$exception = $event->getThrowable();
$error = Error::decodeException($exception);
$error += [
'status_code' => $exception->getStatusCode(),
];
unset($error['@backtrace_string']);
$this->logger->get('client error')
->warning(Error::DEFAULT_ERROR_MESSAGE, $error);
}
/**
* Log all exceptions.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The event to process.
*/
public function onException(ExceptionEvent $event) {
$exception = $event->getThrowable();
$method = 'onError';
// Treat any non-HTTP exception as if it were one, so we log it the same.
if ($exception instanceof HttpExceptionInterface) {
$status_code = $exception->getStatusCode();
$possible_method = 'on' . $status_code;
if (method_exists($this, $possible_method)) {
$method = $possible_method;
}
elseif ($status_code >= 400 && $status_code < 500) {
$method = 'onClientError';
}
}
$this->$method($event);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[KernelEvents::EXCEPTION][] = ['onException', 50];
return $events;
}
}
|