|
7 | 7 | namespace Ibexa\Bundle\Rest\EventListener; |
8 | 8 |
|
9 | 9 | use Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher; |
| 10 | +use Psr\Log\LoggerAwareInterface; |
| 11 | +use Psr\Log\LoggerAwareTrait; |
| 12 | +use Psr\Log\LogLevel; |
10 | 13 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11 | 14 | use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
12 | 15 | use Symfony\Component\HttpKernel\Event\ViewEvent; |
| 16 | +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
13 | 17 | use Symfony\Component\HttpKernel\KernelEvents; |
| 18 | +use Throwable; |
14 | 19 |
|
15 | 20 | /** |
16 | 21 | * REST Response Listener. |
17 | 22 | * |
18 | 23 | * Converts responses from REST controllers to REST Responses, depending on the Accept-Header value. |
19 | 24 | */ |
20 | | -class ResponseListener implements EventSubscriberInterface |
| 25 | +class ResponseListener implements EventSubscriberInterface, LoggerAwareInterface |
21 | 26 | { |
| 27 | + use LoggerAwareTrait; |
| 28 | + |
22 | 29 | /** |
23 | 30 | * @var \Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher |
24 | 31 | */ |
@@ -73,13 +80,31 @@ public function onKernelExceptionView(ExceptionEvent $event) |
73 | 80 | return; |
74 | 81 | } |
75 | 82 |
|
| 83 | + $exception = $event->getThrowable(); |
| 84 | + $this->logException($exception); |
| 85 | + |
76 | 86 | $event->setResponse( |
77 | 87 | $this->viewDispatcher->dispatch( |
78 | 88 | $event->getRequest(), |
79 | | - $event->getThrowable() |
| 89 | + $exception |
80 | 90 | ) |
81 | 91 | ); |
82 | | - $event->stopPropagation(); |
| 92 | + } |
| 93 | + |
| 94 | + private function logException(Throwable $exception): void |
| 95 | + { |
| 96 | + if (!isset($this->logger)) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $logLevel = LogLevel::ERROR; |
| 101 | + if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) { |
| 102 | + $logLevel = LogLevel::CRITICAL; |
| 103 | + } |
| 104 | + |
| 105 | + $this->logger->log($logLevel, $exception->getMessage(), [ |
| 106 | + 'exception' => $exception, |
| 107 | + ]); |
83 | 108 | } |
84 | 109 | } |
85 | 110 |
|
|
0 commit comments