merged branch fabpot/exception-logging (PR #6503)

This PR was merged into the master branch.

Commits
-------

0a42501 [HttpKernel] tweaked logging in the exception listener
1a6c9b3 [HttpKernel] refactored logging in the exception listener

Discussion
----------

[HttpKernel] refactored logging in the exception listener

* avoid code duplication
* allow easier overloading of the default behavior
This commit is contained in:
Fabien Potencier 2012-12-28 23:54:30 +01:00
commit 57ac683d35
2 changed files with 24 additions and 24 deletions

View File

@ -49,23 +49,12 @@ class ExceptionListener implements EventSubscriberInterface
$exception = $event->getException(); $exception = $event->getException();
$request = $event->getRequest(); $request = $event->getRequest();
if (null !== $this->logger) { $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
$message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine());
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->crit($message);
} else {
$this->logger->err($message);
}
} else {
error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
}
$logger = $this->logger instanceof DebugLoggerInterface ? $this->logger : null;
$attributes = array( $attributes = array(
'_controller' => $this->controller, '_controller' => $this->controller,
'exception' => FlattenException::create($exception), 'exception' => FlattenException::create($exception),
'logger' => $logger, 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
'format' => $request->getRequestFormat(), 'format' => $request->getRequestFormat(),
); );
@ -75,16 +64,7 @@ class ExceptionListener implements EventSubscriberInterface
try { try {
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
} catch (\Exception $e) { } catch (\Exception $e) {
$message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()); $this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false);
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->crit($message);
} else {
$this->logger->err($message);
}
} else {
error_log($message);
}
// set handling to false otherwise it wont be able to handle further more // set handling to false otherwise it wont be able to handle further more
$handling = false; $handling = false;
@ -104,4 +84,25 @@ class ExceptionListener implements EventSubscriberInterface
KernelEvents::EXCEPTION => array('onKernelException', -128), KernelEvents::EXCEPTION => array('onKernelException', -128),
); );
} }
/**
* Logs an exception.
*
* @param \Exception $exception The original \Exception instance
* @param string $message The error message to log
* @param Boolean $original False when the handling of the exception thrown another exception
*/
protected function logException(\Exception $exception, $message, $original = true)
{
$isCritical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
if (null !== $this->logger) {
if ($isCritical) {
$this->logger->crit($message);
} else {
$this->logger->err($message);
}
} elseif (!$original || $isCritical) {
error_log($message);
}
}
} }

View File

@ -111,7 +111,6 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
array($event, $event2) array($event, $event2)
); );
} }
} }
class TestLogger extends Logger implements DebugLoggerInterface class TestLogger extends Logger implements DebugLoggerInterface