[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 13:22:56 +01:00
parent c0e341c618
commit 1a6c9b3143
2 changed files with 23 additions and 24 deletions

View File

@ -49,23 +49,12 @@ class ExceptionListener implements EventSubscriberInterface
$exception = $event->getException();
$request = $event->getRequest();
if (null !== $this->logger) {
$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;
$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
$attributes = array(
'_controller' => $this->controller,
'exception' => FlattenException::create($exception),
'logger' => $logger,
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
'format' => $request->getRequestFormat(),
);
@ -75,16 +64,7 @@ class ExceptionListener implements EventSubscriberInterface
try {
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
} catch (\Exception $e) {
$message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage());
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->crit($message);
} else {
$this->logger->err($message);
}
} else {
error_log($message);
}
$this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false);
// set handling to false otherwise it wont be able to handle further more
$handling = false;
@ -104,4 +84,24 @@ class ExceptionListener implements EventSubscriberInterface
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)
{
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->crit($message);
} else {
$this->logger->err($message);
}
} else {
error_log($message);
}
}
}

View File

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