Commit 4945dd0f authored by Fabien Potencier's avatar Fabien Potencier

merged branch davedevelopment/lower-priority-for-exception-logging (PR #753)

This PR was squashed before being merged into the master branch (closes #753).

Discussion
----------

Lower the priority of the exception logging

Fixes #752

I'd like this to be lower, `Symfony\Component\HttpKernel\EventListener\ExceptionListener` has this functionality at [-128](https://github.com/webfactory/symfony/blob/6b64c03e4a92d071263b3c5c765de166106f2d2e/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php#L84), but the default priority for handlers added with `Silex\Application::error` fire at -8, so I thought there would be fewer wtfs if we stay above that?

Commits
-------

b6f72c2d Lower the priority of the exception logging
parents 91091374 b6f72c2d
...@@ -70,6 +70,10 @@ class MonologServiceProvider implements ServiceProviderInterface ...@@ -70,6 +70,10 @@ class MonologServiceProvider implements ServiceProviderInterface
$app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri()); $app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri());
}); });
/*
* Priority -4 is used to come after those from SecurityServiceProvider (0)
* but before the error handlers added with Silex\Application::error (defaults to -8)
*/
$app->error(function (\Exception $e) use ($app) { $app->error(function (\Exception $e) use ($app) {
$message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()); $message = sprintf('%s: %s (uncaught exception) at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
if ($e instanceof HttpExceptionInterface && $e->getStatusCode() < 500) { if ($e instanceof HttpExceptionInterface && $e->getStatusCode() < 500) {
...@@ -77,7 +81,7 @@ class MonologServiceProvider implements ServiceProviderInterface ...@@ -77,7 +81,7 @@ class MonologServiceProvider implements ServiceProviderInterface
} else { } else {
$app['monolog']->addCritical($message, array('exception' => $e)); $app['monolog']->addCritical($message, array('exception' => $e));
} }
}, 255); }, -4);
$app->after(function (Request $request, Response $response) use ($app) { $app->after(function (Request $request, Response $response) use ($app) {
$app['monolog']->addInfo('< '.$response->getStatusCode()); $app['monolog']->addInfo('< '.$response->getStatusCode());
......
...@@ -94,6 +94,31 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -94,6 +94,31 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertMatchingRecord($pattern, Logger::CRITICAL, $app['monolog.handler']); $this->assertMatchingRecord($pattern, Logger::CRITICAL, $app['monolog.handler']);
} }
public function testErrorLoggingGivesWayToSecurityExceptionHandling()
{
$app = $this->getApplication();
$app['monolog.level'] = Logger::ERROR;
$app->register(new \Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'admin' => array(
'pattern' => '^/admin',
'http' => true,
'users' => array(),
),
),
));
$app->get("/admin", function () {
return "SECURE!";
});
$request = Request::create("/admin");
$app->run($request);
$this->assertEmpty($app['monolog.handler']->getRecords(), "Expected no logging to occur");
}
protected function assertMatchingRecord($pattern, $level, $handler) protected function assertMatchingRecord($pattern, $level, $handler)
{ {
$found = false; $found = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment