diff --git a/.travis.yml b/.travis.yml index 5cb3e44f..f4d2e4d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,6 @@ matrix: - php: 5.3 env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak # Test against Symfony LTS versions - - php: 5.6 - env: SYMFONY_VERSION="2.3.*" - php: 5.6 env: SYMFONY_VERSION="2.7.*" - php: 5.6 diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index fea47372..bcc3bf79 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -320,8 +320,11 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (isset($handler['activation_strategy'])) { $activation = new Reference($handler['activation_strategy']); } elseif (!empty($handler['excluded_404s'])) { - $activationDef = new Definition('%monolog.activation_strategy.not_found.class%', array($handler['excluded_404s'], $handler['action_level'])); - $activationDef->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false))); + $activationDef = new Definition('%monolog.activation_strategy.not_found.class%', array( + new Reference('request_stack'), + $handler['excluded_404s'], + $handler['action_level'] + )); $container->setDefinition($handlerId.'.not_found_strategy', $activationDef); $activation = new Reference($handlerId.'.not_found_strategy'); } else { diff --git a/NotFoundActivationStrategy.php b/NotFoundActivationStrategy.php deleted file mode 100644 index 4e94be2a..00000000 --- a/NotFoundActivationStrategy.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\MonologBundle; - -use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\HttpFoundation\Request; - -/** - * Activation strategy that ignores 404s for certain URLs - * - * Configure with: - * - * monolog: - * handlers: - * main: - * type: fingers_crossed - * action_level: error - * handler: nested - * excluded_404s: - * - ^/foo/ - * - * @author Jordi Boggiano - */ -class NotFoundActivationStrategy extends ErrorLevelActivationStrategy -{ - private $blacklist; - private $request; - - public function __construct(array $excludedUrls, $actionLevel) - { - parent::__construct($actionLevel); - $this->blacklist = '{('.implode('|', $excludedUrls).')}i'; - } - - public function isHandlerActivated(array $record) - { - $isActivated = parent::isHandlerActivated($record); - if ( - $isActivated - && $this->request - && isset($record['context']['exception']) - && $record['context']['exception'] instanceof HttpException - && $record['context']['exception']->getStatusCode() == 404 - ) { - return !preg_match($this->blacklist, $this->request->getPathInfo()); - } - - return $isActivated; - } - - public function setRequest(Request $req = null) - { - $this->request = $req; - } -} diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index 6fd638e3..bd13fa67 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -39,7 +39,7 @@ Monolog\Handler\LogglyHandler Monolog\Handler\LogEntriesHandler Monolog\Handler\WhatFailureGroupHandler - Symfony\Bundle\MonologBundle\NotFoundActivationStrategy + Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy Monolog\Handler\FingersCrossedHandler Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index 35074bb3..f9722d30 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -338,7 +338,31 @@ public function testLogglyHandler() )))); $handler = $container->getDefinition('monolog.handler.loggly'); $this->assertDICDefinitionMethodCallAt(0, $handler, 'setTag', array('foo,bar')); + } + + public function testFingersCrossedHandlerWhenExcluded404sAreSpecified() + { + $container = $this->getContainer(array(array('handlers' => array( + 'main' => array('type' => 'fingers_crossed', 'handler' => 'nested', 'excluded_404s' => array('^/foo', '^/bar')), + 'nested' => array('type' => 'stream', 'path' => '/tmp/symfony.log') + )))); + $this->assertTrue($container->hasDefinition('monolog.logger')); + $this->assertTrue($container->hasDefinition('monolog.handler.main')); + $this->assertTrue($container->hasDefinition('monolog.handler.nested')); + $this->assertTrue($container->hasDefinition('monolog.handler.main.not_found_strategy')); + + $logger = $container->getDefinition('monolog.logger'); + $this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%')); + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); + + $strategy = $container->getDefinition('monolog.handler.main.not_found_strategy'); + $this->assertDICDefinitionClass($strategy, '%monolog.activation_strategy.not_found.class%'); + $this->assertDICConstructorArguments($strategy, array(new Reference('request_stack'), array('^/foo', '^/bar'), \Monolog\Logger::WARNING)); + + $handler = $container->getDefinition('monolog.handler.main'); + $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); + $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), new Reference('monolog.handler.main.not_found_strategy'), 0, true, true, null)); } protected function getContainer(array $config = array()) diff --git a/Tests/NotFoundActivationStrategyTest.php b/Tests/NotFoundActivationStrategyTest.php deleted file mode 100644 index 3d5a90f7..00000000 --- a/Tests/NotFoundActivationStrategyTest.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\MonologBundle\Tests; - -use Symfony\Bundle\MonologBundle\NotFoundActivationStrategy; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Monolog\Logger; - -class NotFoundActivationStrategyTest extends \PHPUnit_Framework_TestCase -{ - /** - * @dataProvider isActivatedProvider - */ - public function testIsActivated($url, $record, $expected) - { - $strategy = new NotFoundActivationStrategy(array('^/foo', 'bar'), Logger::WARNING); - $strategy->setRequest(Request::create($url)); - - $this->assertEquals($expected, $strategy->isHandlerActivated($record)); - } - - public function isActivatedProvider() - { - return array( - array('/test', array('level' => Logger::DEBUG), false), - array('/foo', array('level' => Logger::DEBUG, 'context' => $this->getContextException(404)), false), - array('/baz/bar', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false), - array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false), - array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true), - - array('/test', array('level' => Logger::ERROR), true), - array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), true), - array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true), - ); - } - - protected function getContextException($code) - { - return array('exception' => new HttpException($code)); - } -} diff --git a/composer.json b/composer.json index 36c9b4bc..d2b9312b 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,10 @@ ], "require": { "php": ">=5.3.2", - "symfony/monolog-bridge": "~2.3|~3.0", - "symfony/dependency-injection": "~2.3|~3.0", - "symfony/config": "~2.3|~3.0", - "symfony/http-kernel": "~2.3|~3.0", + "symfony/monolog-bridge": "~2.7|~3.0", + "symfony/dependency-injection": "~2.7|~3.0", + "symfony/config": "~2.7|~3.0", + "symfony/http-kernel": "~2.7|~3.0", "monolog/monolog": "~1.18" }, "require-dev": {