Commit ef44966f authored by Fabien Potencier's avatar Fabien Potencier

merged branch maerlyn/master (PR #756)

This PR was merged into the master branch.

Discussion
----------

Log the target url when redirecting

I've modified the monolog provider to also log the target url when the response is a `RedirectResponse`. Also added a test for it.

Commits
-------

3f746692 Log the target url when redirecting
parents 4945dd0f 3f746692
......@@ -15,6 +15,7 @@ use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bridge\Monolog\Handler\DebugHandler;
......@@ -84,7 +85,11 @@ class MonologServiceProvider implements ServiceProviderInterface
}, -4);
$app->after(function (Request $request, Response $response) use ($app) {
if ($response instanceof RedirectResponse) {
$app['monolog']->addInfo('< '.$response->getStatusCode() . ' ' . $response->getTargetUrl());
} else {
$app['monolog']->addInfo('< '.$response->getStatusCode());
}
});
}
}
......@@ -15,6 +15,7 @@ use Monolog\Handler\TestHandler;
use Monolog\Logger;
use Silex\Application;
use Silex\Provider\MonologServiceProvider;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -94,6 +95,22 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertMatchingRecord($pattern, Logger::CRITICAL, $app['monolog.handler']);
}
public function testRedirectLogging()
{
$app = $this->getApplication();
$app->get('/foo', function () use ($app) {
return new RedirectResponse("/bar", 302);
});
$this->assertFalse($app['monolog.handler']->hasInfoRecords());
$request = Request::create('/foo');
$app->handle($request);
$this->assertTrue($app['monolog.handler']->hasInfo('< 302 /bar'));
}
public function testErrorLoggingGivesWayToSecurityExceptionHandling()
{
$app = $this->getApplication();
......
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