Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c11a8eb

Browse filesBrowse files
simshaunfabpot
authored andcommitted
Add a Monolog activation strategy for ignoring specific HTTP codes
1 parent c9ebbce commit c11a8eb
Copy full SHA for c11a8eb

File tree

2 files changed

+127
-0
lines changed
Filter options

2 files changed

+127
-0
lines changed
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;
13+
14+
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
15+
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use Symfony\Component\HttpFoundation\RequestStack;
17+
18+
/**
19+
* Activation strategy that ignores certain HTTP codes.
20+
*
21+
* @author Shaun Simmons <shaun@envysphere.com>
22+
*/
23+
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy
24+
{
25+
private $exclusions;
26+
private $requestStack;
27+
28+
public function __construct(RequestStack $requestStack, array $exclusions, $actionLevel)
29+
{
30+
parent::__construct($actionLevel);
31+
32+
$this->requestStack = $requestStack;
33+
$this->exclusions = $exclusions;
34+
}
35+
36+
public function isHandlerActivated(array $record)
37+
{
38+
$isActivated = parent::isHandlerActivated($record);
39+
40+
if (
41+
$isActivated
42+
&& isset($record['context']['exception'])
43+
&& $record['context']['exception'] instanceof HttpException
44+
&& ($request = $this->requestStack->getMasterRequest())
45+
) {
46+
foreach ($this->exclusions as $exclusion) {
47+
if ($record['context']['exception']->getStatusCode() !== $exclusion['code']) {
48+
continue;
49+
}
50+
51+
$urlBlacklist = null;
52+
if (count($exclusion['url'])) {
53+
return !preg_match('{('.implode('|', $exclusion['url']).')}i', $request->getPathInfo());
54+
}
55+
56+
return false;
57+
}
58+
}
59+
60+
return $isActivated;
61+
}
62+
}
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;
13+
14+
use Monolog\Logger;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
19+
use Symfony\Component\HttpKernel\Exception\HttpException;
20+
21+
class HttpCodeActivationStrategyTest extends TestCase
22+
{
23+
/**
24+
* @dataProvider isActivatedProvider
25+
*/
26+
public function testIsActivated($url, $record, $expected)
27+
{
28+
$requestStack = new RequestStack();
29+
$requestStack->push(Request::create($url));
30+
31+
$strategy = new HttpCodeActivationStrategy(
32+
$requestStack,
33+
array(
34+
array('code' => 403, 'url' => array()),
35+
array('code' => 404, 'url' => array()),
36+
array('code' => 405, 'url' => array()),
37+
array('code' => 400, 'url' => array('^/400/a', '^/400/b')),
38+
),
39+
Logger::WARNING
40+
);
41+
42+
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
43+
}
44+
45+
public function isActivatedProvider()
46+
{
47+
return array(
48+
array('/test', array('level' => Logger::ERROR), true),
49+
array('/400', array('level' => Logger::ERROR, 'context' => $this->getContextException(400)), true),
50+
array('/400/a', array('level' => Logger::ERROR, 'context' => $this->getContextException(400)), false),
51+
array('/400/b', array('level' => Logger::ERROR, 'context' => $this->getContextException(400)), false),
52+
array('/400/c', array('level' => Logger::ERROR, 'context' => $this->getContextException(400)), true),
53+
array('/401', array('level' => Logger::ERROR, 'context' => $this->getContextException(401)), true),
54+
array('/403', array('level' => Logger::ERROR, 'context' => $this->getContextException(403)), false),
55+
array('/404', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
56+
array('/405', array('level' => Logger::ERROR, 'context' => $this->getContextException(405)), false),
57+
array('/500', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),
58+
);
59+
}
60+
61+
protected function getContextException($code)
62+
{
63+
return array('exception' => new HttpException($code));
64+
}
65+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.