Sfoglia il codice sorgente

[MonologBundle] Added the support for formatters and processors

The formatter can be set for an handler. The argument is the id of a
service implementing Monolog\Formatter\FormatterInterface.

Processors can be set on the logger level or for a specific handler. It
can be either a callable service when the value starts with @ either a
callback.
Christophe Coevoet 14 anni fa
parent
commit
9d3acf3073

+ 25 - 1
src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php

@@ -12,7 +12,6 @@
 namespace Symfony\Bundle\MonologBundle\DependencyInjection;
 
 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
-use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
 
 /**
  * This class contains the configuration information for the bundle
@@ -21,6 +20,7 @@ use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  * sections are normalized, and merged.
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
+ * @author Christophe Coevoet <stof@notk.org>
  */
 class Configuration
 {
@@ -36,6 +36,7 @@ class Configuration
 
         $rootNode
             ->fixXmlConfig('handler')
+            ->fixXmlConfig('processor')
             ->children()
                 ->arrayNode('handlers')
                     ->canBeUnset()
@@ -57,7 +58,9 @@ class Configuration
                             ->scalarNode('action_level')->end() // fingerscrossed specific
                             ->scalarNode('buffer_size')->end() // fingerscrossed specific
                             ->scalarNode('handler')->end() // fingerscrossed specific
+                            ->scalarNode('formatter')->end()
                         ->end()
+                        ->append($this->getProcessorsNode())
                         ->validate()
                             ->ifTrue(function($v) { return 'fingerscrossed' === $v['type'] && !isset($v['handler']); })
                             ->thenInvalid('The handler has to be specified to use a FingersCrossedHandler')
@@ -65,8 +68,29 @@ class Configuration
                     ->end()
                 ->end()
             ->end()
+            ->append($this->getProcessorsNode())
         ;
 
         return $treeBuilder->buildTree();
     }
+
+    private function getProcessorsNode()
+    {
+        $treeBuilder = new TreeBuilder();
+        $node = $treeBuilder->root('processors');
+
+        $node
+            ->canBeUnset()
+            ->performNoDeepMerging()
+            ->useAttributeAsKey('name')
+            ->prototype('scalar')
+                ->beforeNormalization()
+                    ->ifTrue(function($v) { return is_array($v) && isset($v['callback']); })
+                    ->then(function($v){ return $v['callback']; })
+                ->end()
+            ->end()
+        ;
+
+        return $node;
+    }
 }

+ 21 - 0
src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php

@@ -56,6 +56,10 @@ class MonologExtension extends Extension
 
             $logger = $container->getDefinition('monolog.logger_prototype');
 
+            if (!empty ($config['processors'])) {
+                $this->addProcessors($logger, $config['processors']);
+            }
+
             $handlers = array();
             foreach ($config['handlers'] as $name => $handler) {
                 $handlers[] = $this->buildHandler($container, $name, $handler);
@@ -112,6 +116,13 @@ class MonologExtension extends Extension
             ));
             break;
         }
+
+        if (!empty ($handler['formatter'])) {
+            $definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
+        }
+        if (!empty ($handler['processors'])) {
+            $this->addProcessors($definition, $handler['processors']);
+        }
         $container->setDefinition($handlerId, $definition);
 
         return $handlerId;
@@ -136,4 +147,14 @@ class MonologExtension extends Extension
     {
         return sprintf('monolog.handler.%s', $name);
     }
+
+    private function addProcessors(Definition $definition, array $processors)
+    {
+        foreach (array_reverse($processors) as $processor) {
+            if (0 === strpos($processor, '@')) {
+                $processor = new Reference(substr($processor, 1));
+            }
+            $definition->addMethodCall('pushProcessor', array($processor));
+        }
+    }
 }

+ 14 - 14
src/Symfony/Bundle/MonologBundle/Resources/config/schema/monolog-1.0.xsd

@@ -10,21 +10,28 @@
     <xsd:complexType name="config">
         <xsd:choice minOccurs="0" maxOccurs="unbounded">
             <xsd:element name="handler" type="handler" />
+            <xsd:element name="processor" type="processor" />
         </xsd:choice>
     </xsd:complexType>
 
     <xsd:complexType name="handler">
-        <xsd:all>
-            <xsd:element name="handler" maxOccurs="1">
-                <xsd:complexType>
-                    <xsd:attributeGroup ref="handler_attribute" />
-                </xsd:complexType>
-            </xsd:element>
+        <xsd:choice minOccurs="0" maxOccurs="unbounded">
+            <xsd:element name="processor" type="processor" />
         </xsd:all>
-        <xsd:attributeGroup ref="handler_attributes" />
+        <xsd:attribute name="type" type="xsd:string" use="required" />
+        <xsd:attribute name="level" type="level" />
+        <xsd:attribute name="bubble" type="xsd:boolean" />
+        <xsd:attribute name="path" type="xsd:string" />
         <xsd:attribute name="name" type="xsd:string" use="required">
         <xsd:attribute name="action-level" type="level" />
         <xsd:attribute name="buffer-size" type="xsd:integer" />
+        <xsd:attribute name="handler" type="xsd:string" />
+        <xsd:attribute name="formatter" type="xsd:string" />
+    </xsd:complexType>
+
+    <xsd:complexType name="processor">
+        <xsd:attribute name="callback" type="xsd:string" use="required" />
+        <xsd:attribute name="name" type="xsd:string" use="required">
     </xsd:complexType>
 
     <xsd:simpleType name="level">
@@ -40,11 +47,4 @@
             <xsd:enumeration value="400" />
         </xsd:restriction>
     </xsd:simpleType>
-
-    <xsd:attributeGroup name="handler_attributes">
-        <xsd:attribute name="type" type="xsd:string" use="required" />
-        <xsd:attribute name="level" type="level" />
-        <xsd:attribute name="bubble" type="xsd:boolean" />
-        <xsd:attribute name="path" type="xsd:string" />
-    </xsd:attributeGroup>
 </xsd:schema>