Skip to content

Commit 0502453

Browse files
author
William Pottier
committed
Add a nested option on handler config definition
Marked nested handlers with not be pushed to the logger handler array. This options may be usefull when using custom handler (as service type handlers) wich rely on a sub handler defined in the monolog-bundle configuration. Marking this subhandler as nested avoid monolog to directly call it.
1 parent c25e740 commit 0502453

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ public function getConfigTreeBuilder()
592592
->end()
593593
->end()
594594
->scalarNode('formatter')->end()
595+
->booleanNode('nested')->defaultFalse()->end()
595596
->end()
596597
->validate()
597598
->ifTrue(function ($v) { return 'service' === $v['type'] && !empty($v['formatter']); })

DependencyInjection/MonologExtension.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
308308
$handler['passthru_level'] = $this->levelToMonologConst($handler['passthru_level']);
309309
}
310310
$nestedHandlerId = $this->getHandlerId($handler['handler']);
311-
$this->nestedHandlers[] = $nestedHandlerId;
311+
$this->markNestedHandler($nestedHandlerId);
312312

313313
if (isset($handler['activation_strategy'])) {
314314
$activation = new Reference($handler['activation_strategy']);
@@ -339,7 +339,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
339339
}
340340

341341
$nestedHandlerId = $this->getHandlerId($handler['handler']);
342-
$this->nestedHandlers[] = $nestedHandlerId;
342+
$this->markNestedHandler($nestedHandlerId);
343343
$minLevelOrList = !empty($handler['accepted_levels']) ? $handler['accepted_levels'] : $handler['min_level'];
344344

345345
$definition->setArguments(array(
@@ -352,7 +352,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
352352

353353
case 'buffer':
354354
$nestedHandlerId = $this->getHandlerId($handler['handler']);
355-
$this->nestedHandlers[] = $nestedHandlerId;
355+
$this->markNestedHandler($nestedHandlerId);
356356

357357
$definition->setArguments(array(
358358
new Reference($nestedHandlerId),
@@ -368,7 +368,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
368368
$references = array();
369369
foreach ($handler['members'] as $nestedHandler) {
370370
$nestedHandlerId = $this->getHandlerId($nestedHandler);
371-
$this->nestedHandlers[] = $nestedHandlerId;
371+
$this->markNestedHandler($nestedHandlerId);
372372
$references[] = new Reference($nestedHandlerId);
373373
}
374374

@@ -631,6 +631,10 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
631631
throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"', $handler['type'], $name));
632632
}
633633

634+
if (!empty($handler['nested']) && true === $handler['nested']) {
635+
$this->markNestedHandler($handlerId);
636+
}
637+
634638
if (!empty($handler['formatter'])) {
635639
$definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
636640
}
@@ -639,6 +643,15 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
639643
return $handlerId;
640644
}
641645

646+
private function markNestedHandler($nestedHandlerId)
647+
{
648+
if (in_array($nestedHandlerId, $this->nestedHandlers)) {
649+
return;
650+
}
651+
652+
$this->nestedHandlers[] = $nestedHandlerId;
653+
}
654+
642655
private function getHandlerId($name)
643656
{
644657
return sprintf('monolog.handler.%s', $name);

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testProcessSimpleCase()
3636
$this->assertArrayHasKey('foobar', $config['handlers']);
3737
$this->assertEquals('stream', $config['handlers']['foobar']['type']);
3838
$this->assertEquals('/foo/bar', $config['handlers']['foobar']['path']);
39+
$this->assertFalse($config['handlers']['foobar']['nested']);
3940
}
4041

4142
public function provideProcessStringChannels()
@@ -321,6 +322,20 @@ public function testWithFilePermission()
321322
$this->assertSame(0777, $config['handlers']['bar']['file_permission']);
322323
}
323324

325+
public function testWithNestedHandler()
326+
{
327+
$configs = array(
328+
array(
329+
'handlers' => array('foobar' => array('type' => 'stream', 'path' => '/foo/bar', 'nested' => true))
330+
)
331+
);
332+
333+
$config = $this->process($configs);
334+
335+
336+
$this->assertTrue($config['handlers']['foobar']['nested']);
337+
}
338+
324339
/**
325340
* Processes an array of configurations and returns a compiled version.
326341
*

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ public function testLoadWithCustomValues()
5050
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666));
5151
}
5252

53+
public function testLoadWithNestedHandler()
54+
{
55+
$container = $this->getContainer(array(array('handlers' => array(
56+
'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666'),
57+
'nested' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666', 'nested' => true)
58+
))));
59+
$this->assertTrue($container->hasDefinition('monolog.logger'));
60+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
61+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
62+
63+
$logger = $container->getDefinition('monolog.logger');
64+
// Nested handler must not be pushed to logger
65+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
66+
67+
$handler = $container->getDefinition('monolog.handler.custom');
68+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
69+
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666));
70+
}
71+
5372
/**
5473
* @expectedException InvalidArgumentException
5574
*/

0 commit comments

Comments
 (0)