Skip to content

Commit ab8dcdc

Browse files
committed
Fixed bug in tests and in MonologExtension.php (handler priority)
1 parent e09c32b commit ab8dcdc

File tree

5 files changed

+99
-49
lines changed

5 files changed

+99
-49
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,22 @@ public function load(array $configs, ContainerBuilder $container)
4747
$handlers = array();
4848

4949
foreach ($config['handlers'] as $name => $handler) {
50-
$handlers[] = array(
50+
$handlers[$handler['priority']][] = array(
5151
'id' => $this->buildHandler($container, $name, $handler),
52-
'priority' => $handler['priority'],
5352
'channels' => isset($handler['channels']) ? $handler['channels'] : null
5453
);
5554
}
5655

57-
$handlers = array_reverse($handlers);
58-
uasort($handlers, function($a, $b) {
59-
if ($a['priority'] == $b['priority']) {
60-
return 0;
56+
ksort($handlers);
57+
$sortedHandlers = array();
58+
foreach ($handlers as $priorityHandlers) {
59+
foreach (array_reverse($priorityHandlers) as $handler) {
60+
$sortedHandlers[] = $handler;
6161
}
62+
}
6263

63-
return $a['priority'] < $b['priority'] ? -1 : 1;
64-
});
6564
$handlersToChannels = array();
66-
foreach ($handlers as $handler) {
65+
foreach ($sortedHandlers as $handler) {
6766
if (!in_array($handler['id'], $this->nestedHandlers)) {
6867
$handlersToChannels[$handler['id']] = $handler['channels'];
6968
}

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

1414
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
15+
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Reference;
1718

@@ -27,6 +28,7 @@ public function testLoadWithSeveralHandlers()
2728
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
2829

2930
$logger = $container->getDefinition('monolog.logger');
31+
$this->assertCount(2, $logger->getMethodCalls());
3032
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
3133
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
3234

@@ -49,6 +51,7 @@ public function testLoadWithOverwriting()
4951
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
5052

5153
$logger = $container->getDefinition('monolog.logger');
54+
$this->assertCount(2, $logger->getMethodCalls());
5255
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
5356
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
5457

@@ -72,9 +75,10 @@ public function testLoadWithNewAtEnd()
7275
$this->assertTrue($container->hasDefinition('monolog.handler.new'));
7376

7477
$logger = $container->getDefinition('monolog.logger');
75-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.new')));
76-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
77-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
78+
$this->assertCount(3, $logger->getMethodCalls());
79+
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
80+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
81+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.new')));
7882

7983
$handler = $container->getDefinition('monolog.handler.new');
8084
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
@@ -93,10 +97,11 @@ public function testLoadWithNewAndPriority()
9397
$this->assertTrue($container->hasDefinition('monolog.handler.last'));
9498

9599
$logger = $container->getDefinition('monolog.logger');
96-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.last')));
97-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
98-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
99-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.first')));
100+
$this->assertCount(4, $logger->getMethodCalls());
101+
$this->assertDICDefinitionMethodCallAt(3, $logger, 'pushHandler', array(new Reference('monolog.handler.first')));
102+
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
103+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
104+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.last')));
100105

101106
$handler = $container->getDefinition('monolog.handler.main');
102107
$this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%');
@@ -135,6 +140,7 @@ protected function getContainer($fixture)
135140

136141
$container->getCompilerPassConfig()->setOptimizationPasses(array());
137142
$container->getCompilerPassConfig()->setRemovingPasses(array());
143+
$container->addCompilerPass(new LoggerChannelPass());
138144
$container->compile();
139145

140146
return $container;
Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
<?xml version="1.0" ?>
22

3-
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
3+
<container xmlns="http://symfony.com/schema/dic/services"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xmlns:srv="http://symfony.com/schema/dic/services"
5+
xmlns:monolog="http://symfony.com/schema/dic/monolog"
66
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
77
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
88

9-
<config>
10-
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="ERROR">
11-
<channels>
12-
<channel>foo</channel>
13-
</channels>
14-
</handler>
15-
<handler name="main" type="group" handler="nested">
16-
<member>nested</member>
17-
<channels>
18-
<channel>!foo</channel>
19-
<channel>!bar</channel>
20-
</channels>
21-
</handler>
22-
<handler name="nested" type="stream" />
23-
<handler name="extra" type="syslog" ident="monolog" facility="user" level="ALERT" />
24-
<handler name="more" type="native_mailer" to-email="[email protected]" from-email="[email protected]" subject="Monolog report" level="CRITICAL">
25-
<channels type="inclusive">
26-
<channel>security</channel>
27-
<channel>doctrine</channel>
28-
</channels>
29-
</handler>
30-
</config>
31-
</srv:container>
9+
<services>
10+
<service id="security_logger" class="Foo">
11+
<tag name="monolog.logger" channel="security" />
12+
</service>
13+
14+
<service id="doctrine_logger" class="Foo">
15+
<tag name="monolog.logger" channel="doctrine" />
16+
</service>
17+
18+
<service id="foo_logger" class="Foo">
19+
<tag name="monolog.logger" channel="foo" />
20+
</service>
21+
22+
<service id="bar_logger" class="Foo">
23+
<tag name="monolog.logger" channel="bar" />
24+
</service>
25+
</services>
26+
27+
<monolog:config>
28+
<monolog:handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="ERROR">
29+
<monolog:channels>
30+
<monolog:channel>foo</monolog:channel>
31+
</monolog:channels>
32+
</monolog:handler>
33+
<monolog:handler name="main" type="group" handler="nested">
34+
<monolog:member>nested</monolog:member>
35+
<monolog:channels>
36+
<monolog:channel>!foo</monolog:channel>
37+
<monolog:channel>!bar</monolog:channel>
38+
</monolog:channels>
39+
</monolog:handler>
40+
<monolog:handler name="nested" type="stream" />
41+
<monolog:handler name="extra" type="syslog" ident="monolog" facility="user" level="ALERT" />
42+
<monolog:handler name="more" type="native_mailer" to-email="[email protected]" from-email="[email protected]" subject="Monolog report" level="CRITICAL">
43+
<monolog:channels type="inclusive">
44+
<monolog:channel>security</monolog:channel>
45+
<monolog:channel>doctrine</monolog:channel>
46+
</monolog:channels>
47+
</monolog:handler>
48+
</monolog:config>
49+
</container>

Tests/DependencyInjection/Fixtures/yml/handlers_with_channels.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
services:
2+
security_logger:
3+
class: Foo
4+
tags: [{ name: monolog.logger, channel: security }]
5+
6+
doctrine_logger:
7+
class: Foo
8+
tags: [{ name: monolog.logger, channel: doctrine }]
9+
10+
foo_logger:
11+
class: Foo
12+
tags: [{ name: monolog.logger, channel: foo }]
13+
14+
bar_logger:
15+
class: Foo
16+
tags: [{ name: monolog.logger, channel: bar }]
17+
118
monolog:
219
handlers:
320
custom:

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

1414
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
15+
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Reference;
1718

1819
class MonologExtensionTest extends DependencyInjectionTest
1920
{
2021
public function testLoadWithDefault()
2122
{
22-
$container = new ContainerBuilder();
23-
$loader = new MonologExtension();
24-
25-
$loader->load(array(array('handlers' => array('main' => array('type' => 'stream')))), $container);
23+
$container = $this->getContainer(array(array('handlers' => array('main' => array('type' => 'stream')))));
24+
2625
$this->assertTrue($container->hasDefinition('monolog.logger'));
2726
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
2827

@@ -36,10 +35,7 @@ public function testLoadWithDefault()
3635

3736
public function testLoadWithCustomValues()
3837
{
39-
$container = new ContainerBuilder();
40-
$loader = new MonologExtension();
41-
42-
$loader->load(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR')))), $container);
38+
$container = $this->getContainer(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR')))));
4339
$this->assertTrue($container->hasDefinition('monolog.logger'));
4440
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
4541

@@ -128,4 +124,18 @@ public function testExceptionWhenUsingDebugName()
128124

129125
$loader->load(array(array('handlers' => array('debug' => array('type' => 'stream')))), $container);
130126
}
127+
128+
protected function getContainer(array $config = array())
129+
{
130+
$container = new ContainerBuilder();
131+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
132+
$container->getCompilerPassConfig()->setRemovingPasses(array());
133+
$container->addCompilerPass(new LoggerChannelPass());
134+
135+
$loader = new MonologExtension();
136+
$loader->load($config, $container);
137+
$container->compile();
138+
139+
return $container;
140+
}
131141
}

0 commit comments

Comments
 (0)