diff --git a/UPDATE.md b/UPDATE.md index e9054c67d4..19e963f3c4 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -9,6 +9,15 @@ timeline closely anyway. RC4 to RC5 ---------- +* The configuration of MonologBundle has been refactored. + + * Only services are supported for the processors. They are now registered + using the `monolog.processor` tag which accept three optionnal attributes: + + * `handler`: the name of an handler to register it only for a specific handler + * `channel`: to register it only for one logging channel (exclusive with `handler`) + * `method`: The method used to process the record (`__invoke` is used if not set) + * To avoid security issues, HTTP headers coming from proxies are not trusted anymore by default (like `HTTP_X_FORWARDED_FOR`, `X_FORWARDED_PROTO`, and `X_FORWARDED_HOST`). If your application is behind a reverse proxy, add the @@ -20,7 +29,7 @@ RC4 to RC5 RC3 to RC4 ---------- -* Annotation classes must be annotated with @Annotation +* Annotation classes must be annotated with @Annotation (see the validator constraints for examples) * Annotations are not using the PHP autoloading but their own mechanism. This diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php new file mode 100644 index 0000000000..83b2b13fec --- /dev/null +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Registers processors in Monolg loggers or handlers. + * + * @author Christophe Coevoet + */ +class AddProcessorsPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('monolog.logger')) { + return; + } + + foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) { + foreach ($tags as $tag) { + if (!empty($tag['channel']) && !empty($tag['handler'])) { + throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id)); + } + + if (!empty($tag['handler'])) { + $definition = $container->getDefinition(sprintf('monolog.handler.%s', $tag['handler'])); + } elseif (!empty($tag['channel'])) { + if ('app' === $tag['channel']) { + $definition = $container->getDefinition('monolog.logger'); + } else { + $definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel'])); + } + } else { + $definition = $container->getDefinition('monolog.logger_prototype'); + } + + if (!empty($tag['method'])) { + $processor = array(new Reference($id), $tag['method']); + } else { + // If no method is defined, fallback to use __invoke + $processor = new Reference($id); + } + $definition->addMethodCall('pushProcessor', array($processor)); + } + } + } +} diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php index d5357e98b3..619824c8ee 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php @@ -37,7 +37,6 @@ class Configuration implements ConfigurationInterface $rootNode ->fixXmlConfig('handler') - ->fixXmlConfig('processor') ->children() ->arrayNode('handlers') ->canBeUnset() @@ -77,7 +76,6 @@ class Configuration implements ConfigurationInterface ->scalarNode('email_prototype')->end() // swift_mailer ->scalarNode('formatter')->end() ->end() - ->append($this->getProcessorsNode()) ->validate() ->ifTrue(function($v) { return ('fingers_crossed' === $v['type'] || 'buffer' === $v['type']) && 1 !== count($v['handler']); }) ->thenInvalid('The handler has to be specified to use a FingersCrossedHandler or BufferHandler') @@ -101,23 +99,8 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->end() - ->append($this->getProcessorsNode()) ; return $treeBuilder; } - - private function getProcessorsNode() - { - $treeBuilder = new TreeBuilder(); - $node = $treeBuilder->root('processors'); - - $node - ->canBeUnset() - ->performNoDeepMerging() - ->prototype('scalar')->end() - ; - - return $node; - } } diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php index c53f217282..b4c042eb5b 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php @@ -49,10 +49,6 @@ class MonologExtension extends Extension $logger = $container->getDefinition('monolog.logger_prototype'); - if (!empty($config['processors'])) { - $this->addProcessors($container, $logger, $config['processors']); - } - $handlers = array(); foreach ($config['handlers'] as $name => $handler) { $handlers[] = array('id' => $this->buildHandler($container, $name, $handler), 'priority' => $handler['priority'] ); @@ -238,9 +234,6 @@ class MonologExtension extends Extension if (!empty($handler['formatter'])) { $definition->addMethodCall('setFormatter', array(new Reference($handler['formatter']))); } - if (!empty($handler['processors'])) { - $this->addProcessors($container, $definition, $handler['processors']); - } $container->setDefinition($handlerId, $definition); return $handlerId; @@ -251,13 +244,6 @@ class MonologExtension extends Extension return sprintf('monolog.handler.%s', $name); } - private function addProcessors(ContainerBuilder $container, Definition $definition, array $processors) - { - foreach (array_reverse($processors) as $processor) { - $definition->addMethodCall('pushProcessor', array($this->parseDefinition($processor, $container))); - } - } - private function parseDefinition($definition, ContainerBuilder $container = null) { if (0 === strpos($definition, '@')) { diff --git a/src/Symfony/Bundle/MonologBundle/MonologBundle.php b/src/Symfony/Bundle/MonologBundle/MonologBundle.php index e51ddf1e32..013f3cfa44 100644 --- a/src/Symfony/Bundle/MonologBundle/MonologBundle.php +++ b/src/Symfony/Bundle/MonologBundle/MonologBundle.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DebugHandlerPass; +use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass; /** * Bundle. @@ -29,5 +30,6 @@ class MonologBundle extends Bundle $container->addCompilerPass(new LoggerChannelPass()); $container->addCompilerPass(new DebugHandlerPass()); + $container->addCompilerPass(new AddProcessorsPass()); } } diff --git a/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml b/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml index 569db8f2e0..8b42f4d965 100644 --- a/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml +++ b/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml @@ -18,8 +18,6 @@ Symfony\Bridge\Monolog\Handler\DebugHandler Monolog\Handler\SwiftMailerHandler Monolog\Handler\NativeMailerHandler - Symfony\Bridge\Monolog\Processor\WebProcessor - Monolog\Processor\IntrospectionProcessor @@ -32,11 +30,5 @@ - - - - - - diff --git a/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php index 06effcd499..903217d708 100644 --- a/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php +++ b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php @@ -35,21 +35,6 @@ class MonologExtensionTest extends TestCase $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true)); } - public function testLoadWithProcessor() - { - $container = new ContainerBuilder(); - $loader = new MonologExtension(); - - $loader->load(array(array('handlers' => array('main' => array('type' => 'stream', 'processors' => array('@monolog.processor.web'))))), $container); - $this->assertTrue($container->hasDefinition('monolog.handler.main')); - - $handler = $container->getDefinition('monolog.handler.main'); - $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.web'))); - - $this->assertTrue($container->getDefinition('monolog.processor.web')->isPublic()); - $this->assertFalse($container->getDefinition('monolog.processor.introspection')->isPublic()); - } - public function testLoadWithCustomValues() { $container = new ContainerBuilder();