Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b9ee9e

Browse files
committed
Restore ExceptionListener as exception rendering listener, add BC mode
1 parent f7d8fcf commit 9b9ee9e

File tree

9 files changed

+266
-169
lines changed

9 files changed

+266
-169
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<tag name="monolog.logger" channel="request" />
7878
<argument type="service" id="logger" on-invalid="null" />
7979
<argument>%framework.exception_listener.http_log_levels%</argument>
80+
<argument type="service" id="twig.exception_listener" on-invalid="null" />
8081
</service>
8182

8283
</services>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
249249
),
250250
),
251251
),
252+
'http_exception_log_levels' => array(),
252253
);
253254
}
254255
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@
123123
<argument type="service" id="workflow.registry" />
124124
</service>
125125

126-
<service id="twig.exception_listener" class="Symfony\Component\HttpKernel\EventListener\RenderControllerExceptionListener">
126+
<service id="twig.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
127127
<tag name="kernel.event_subscriber" />
128128
<tag name="monolog.logger" channel="request" />
129129
<argument>%twig.exception_listener.controller%</argument>
130130
<argument type="service" id="logger" on-invalid="null" />
131+
<argument>false</argument>
131132
</service>
132133

133134
<service id="twig.controller.exception" class="Symfony\Bundle\TwigBundle\Controller\ExceptionController" public="true">

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,22 @@
2323
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2424
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2525

26-
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0, use "%s" or "%s" instead.', ExceptionListener::class, RenderControllerExceptionListener::class, LoggingExceptionListener::class), E_USER_DEPRECATED);
27-
2826
/**
2927
* ExceptionListener.
3028
*
3129
* @author Fabien Potencier <[email protected]>
32-
*
33-
* @deprecated since Symfony 4.1, use RenderControllerExceptionListener or LoggingExceptionListener instead
3430
*/
3531
class ExceptionListener implements EventSubscriberInterface
3632
{
3733
protected $controller;
3834
protected $logger;
35+
private $isBCExceptionLoggingEnabled;
3936

40-
public function __construct($controller, LoggerInterface $logger = null)
37+
public function __construct($controller, LoggerInterface $logger = null/*, bool $isBCExceptionLoggingEnabled = true*/)
4138
{
4239
$this->controller = $controller;
4340
$this->logger = $logger;
41+
$this->isBCExceptionLoggingEnabled = (func_num_args() >= 3) ? (bool) func_get_arg(2) : true;
4442
}
4543

4644
public function onKernelException(GetResponseForExceptionEvent $event)
@@ -49,14 +47,21 @@ public function onKernelException(GetResponseForExceptionEvent $event)
4947
$request = $event->getRequest();
5048
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null;
5149

52-
$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
50+
if ($this->isBCExceptionLoggingEnabled) {
51+
$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
52+
}
5353

5454
$request = $this->duplicateRequest($exception, $request);
5555

5656
try {
5757
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
5858
} catch (\Exception $e) {
59-
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()));
59+
$message = sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
60+
if ($this->isBCExceptionLoggingEnabled) {
61+
$this->logException($e, $message);
62+
} elseif (null !== $this->logger) {
63+
$this->logger->critical($message, array('exception' => $e));
64+
}
6065

6166
$wrapper = $e;
6267

@@ -96,9 +101,13 @@ public static function getSubscribedEvents()
96101
*
97102
* @param \Exception $exception The \Exception instance
98103
* @param string $message The error message to log
104+
*
105+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use LoggingExceptionListener instead to log exceptions.
99106
*/
100107
protected function logException(\Exception $exception, $message)
101108
{
109+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.1 and will be removed in 5.0. Use %s instead.', __METHOD__, LoggingExceptionListener::class), E_USER_DEPRECATED);
110+
102111
if (null !== $this->logger) {
103112
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
104113
$this->logger->critical($message, array('exception' => $exception));
@@ -128,4 +137,14 @@ protected function duplicateRequest(\Exception $exception, Request $request)
128137

129138
return $request;
130139
}
140+
141+
/**
142+
* @deprecated since Symfony 4.1, to be removed in 5.0.
143+
*
144+
* @internal
145+
*/
146+
public function isLoggingExceptions(): bool
147+
{
148+
return $this->isBCExceptionLoggingEnabled;
149+
}
131150
}

src/Symfony/Component/HttpKernel/EventListener/LoggingExceptionListener.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@ class LoggingExceptionListener implements EventSubscriberInterface
2323
{
2424
protected $logger;
2525
protected $httpStatusCodeLogLevel;
26+
private $isDisabled;
2627

27-
public function __construct(LoggerInterface $logger = null, array $httpStatusCodeLogLevel = array())
28+
public function __construct(LoggerInterface $logger = null, array $httpStatusCodeLogLevel = array()/*, ExceptionListener $exceptionListener = null */)
2829
{
2930
$this->logger = $logger ?: new NullLogger();
3031
$this->httpStatusCodeLogLevel = $httpStatusCodeLogLevel;
32+
33+
if (func_num_args() >= 3) {
34+
$exceptionListener = func_get_arg(2);
35+
$this->isDisabled = ($exceptionListener instanceof ExceptionListener) ? $exceptionListener->isLoggingExceptions() : false;
36+
if ($this->isDisabled) {
37+
$this->logger->debug('Logging disabled for backwards compatibility, ExceptionListener is already logging exceptions.');
38+
}
39+
}
3140
}
3241

3342
public function logKernelException(GetResponseForExceptionEvent $event)
3443
{
44+
if ($this->isDisabled) {
45+
return;
46+
}
47+
3548
$exception = $event->getException();
3649
$level = $this->getExceptionLogLevel($exception);
3750
$message = sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine());

src/Symfony/Component/HttpKernel/EventListener/RenderControllerExceptionListener.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)