Skip to content

Commit ec44bd3

Browse files
committed
Fixed the handling of handlers per channel to support merging configs
Ref #3
1 parent 509bc00 commit ec44bd3

File tree

7 files changed

+108
-15
lines changed

7 files changed

+108
-15
lines changed

DependencyInjection/Compiler/LoggerChannelPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function processChannels($configuration)
6565
if ('inclusive' === $configuration['type']) {
6666
return $configuration['elements'];
6767
}
68+
6869
return array_diff($this->channels, $configuration['elements']);
6970
}
7071

DependencyInjection/Configuration.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,26 @@ public function getConfigTreeBuilder()
8989
->fixXmlConfig('channel', 'elements')
9090
->canBeUnset()
9191
->beforeNormalization()
92+
->ifString()
93+
->then(function($v) { return array('elements' => array($v)); })
94+
->end()
95+
->beforeNormalization()
96+
->ifTrue(function($v) { return is_array($v) && is_numeric(key($v)); })
97+
->then(function($v) { return array('elements' => $v); })
98+
->end()
99+
->validate()
100+
->ifTrue(function($v) { return empty($v); })
101+
->thenUnset()
102+
->end()
103+
->validate()
92104
->always(function ($v) {
93-
94-
if (is_string($v)) {
95-
$v = array($v);
96-
}
97-
98-
if (null === $v || count($v) == 0) {
99-
return null;
100-
}
101-
105+
$isExclusive = null;
102106
if (isset($v['type'])) {
103-
return $v;
107+
$isExclusive = 'exclusive' === $v['type'];
104108
}
105109

106-
$isExclusive = null;
107110
$elements = array();
108-
foreach ($v as $element) {
111+
foreach ($v['elements'] as $element) {
109112
if (0 === strpos($element, '!')) {
110113
if (false === $isExclusive) {
111114
throw new InvalidConfigurationException('Cannot combine exclusive/inclusive definitions in channels list.');

DependencyInjection/MonologExtension.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public function load(array $configs, ContainerBuilder $container)
4444
$loader->load('monolog.xml');
4545
$container->setAlias('logger', 'monolog.logger');
4646

47-
$logger = $container->getDefinition('monolog.logger_prototype');
48-
4947
$handlers = array();
5048

5149
foreach ($config['handlers'] as $name => $handler) {

Resources/config/schema/monolog-1.0.xsd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<xsd:sequence>
1818
<xsd:element name="email-prototype" type="email-prototype" minOccurs="0" maxOccurs="1" />
1919
<xsd:element name="member" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
20+
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
2021
</xsd:sequence>
2122
<xsd:attribute name="type" type="xsd:string" />
2223
<xsd:attribute name="priority" type="xsd:integer" />
@@ -46,6 +47,13 @@
4647
<xsd:enumeration value="critical" />
4748
<xsd:enumeration value="alert" />
4849

50+
<xsd:enumeration value="DEBUG" />
51+
<xsd:enumeration value="INFO" />
52+
<xsd:enumeration value="WARNING" />
53+
<xsd:enumeration value="ERROR" />
54+
<xsd:enumeration value="CRITICAL" />
55+
<xsd:enumeration value="ALERT" />
56+
4957
<xsd:enumeration value="100" />
5058
<xsd:enumeration value="200" />
5159
<xsd:enumeration value="300" />
@@ -62,8 +70,15 @@
6270

6371
<xsd:complexType name="channels">
6472
<xsd:sequence>
65-
<xsd:element name="type" type="xsd:string" minOccurs="1" maxOccurs="1" />
6673
<xsd:element name="channel" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
6774
</xsd:sequence>
75+
<xsd:attribute name="type" type="xsd:string" />
6876
</xsd:complexType>
77+
78+
<xsd:simpleType name="channel_type">
79+
<xsd:restriction base="xsd:string">
80+
<xsd:enumeration value="inclusive" />
81+
<xsd:enumeration value="exclusive" />
82+
</xsd:restriction>
83+
</xsd:simpleType>
6984
</xsd:schema>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" ?>
2+
3+
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
8+
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>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
monolog:
2+
handlers:
3+
custom:
4+
type: stream
5+
path: /tmp/symfony.log
6+
bubble: false
7+
level: ERROR
8+
channels: foo
9+
main:
10+
type: group
11+
members: [nested]
12+
channels: ["!foo", "!bar"]
13+
nested:
14+
type: stream
15+
extra:
16+
type: syslog
17+
ident: monolog
18+
facility: user
19+
level: ALERT
20+
more:
21+
type: native_mailer
22+
23+
from_email: [email protected]
24+
subject: Monolog report
25+
level: CRITICAL
26+
channels:
27+
type: inclusive
28+
elements:
29+
- security
30+
- doctrine

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ public function testLoadWithNewAndPriority()
146146
$this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true));
147147
}
148148

149+
public function testHandlersWithChannels()
150+
{
151+
$container = $this->getContainer('handlers_with_channels');
152+
153+
$this->assertEquals(
154+
array(
155+
'monolog.handler.custom' => array('type' => 'inclusive', 'elements' => array('foo')),
156+
'monolog.handler.main' => array('type' => 'exclusive', 'elements' => array('foo', 'bar')),
157+
'monolog.handler.extra' => null,
158+
'monolog.handler.more' => array('type' => 'inclusive', 'elements' => array('security', 'doctrine')),
159+
),
160+
$container->getParameter('monolog.handlers_to_channels')
161+
);
162+
}
163+
149164
/**
150165
* @expectedException InvalidArgumentException
151166
*/

0 commit comments

Comments
 (0)