Skip to content

Commit e897bec

Browse files
author
Martin Parsiegla
committedApr 27, 2012
Added possibility to directly configure the gelf publisher.
1 parent 120ef17 commit e897bec

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed
 

‎DependencyInjection/Configuration.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,25 @@ public function getConfigTreeBuilder()
6767
->booleanNode('stop_buffering')->defaultTrue()->end()// fingers_crossed
6868
->scalarNode('buffer_size')->defaultValue(0)->end() // fingers_crossed and buffer
6969
->scalarNode('handler')->end() // fingers_crossed and buffer
70-
->scalarNode('publisher')->end() // gelf
70+
->arrayNode('publisher')
71+
->canBeUnset()
72+
->beforeNormalization()
73+
->ifString()
74+
->then(function($v) { return array('id'=> $v); })
75+
->end()
76+
->children()
77+
->scalarNode('id')->end()
78+
->scalarNode('hostname')->end()
79+
->scalarNode('port')->defaultValue(12201)->end()
80+
->scalarNode('chunk_size')->defaultValue(1420)->end()
81+
->end()
82+
->validate()
83+
->ifTrue(function($v) {
84+
return !isset($v['id']) && !isset($v['hostname']);
85+
})
86+
->thenInvalid('What must be set is either the hostname or the id.')
87+
->end()
88+
->end() // gelf
7189
->arrayNode('members') // group
7290
->canBeUnset()
7391
->performNoDeepMerging()
@@ -159,6 +177,10 @@ public function getConfigTreeBuilder()
159177
->ifTrue(function($v) { return 'service' === $v['type'] && !isset($v['id']); })
160178
->thenInvalid('The id has to be specified to use a service as handler')
161179
->end()
180+
->validate()
181+
->ifTrue(function($v) { return 'gelf' === $v['type'] && !isset($v['publisher']); })
182+
->thenInvalid('The publisher has to be specified to use a GelfHandler')
183+
->end()
162184
->end()
163185
->validate()
164186
->ifTrue(function($v) { return isset($v['debug']); })

‎DependencyInjection/MonologExtension.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,22 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
130130
break;
131131

132132
case 'gelf':
133+
if (isset($handler['publisher']['id'])) {
134+
$publisherId = $handler['publisher']['id'];
135+
} else {
136+
$publisher = new Definition("%monolog.gelf.publisher.class%", array(
137+
$handler['publisher']['hostname'],
138+
$handler['publisher']['port'],
139+
$handler['publisher']['chunk_size'],
140+
));
141+
142+
$publisherId = 'monolog.gelf.publisher';
143+
$publisher->setPublic(false);
144+
$container->setDefinition($publisherId, $publisher);
145+
}
146+
133147
$definition->setArguments(array(
134-
new Reference($handler['publisher']),
148+
new Reference($publisherId),
135149
$handler['level'],
136150
$handler['bubble'],
137151
));

‎Resources/config/monolog.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<parameters>
88
<parameter key="monolog.logger.class">Symfony\Bridge\Monolog\Logger</parameter>
9+
<parameter key="monolog.gelf.publisher.class">Gelf\MessagePublisher</parameter>
910
<parameter key="monolog.handler.stream.class">Monolog\Handler\StreamHandler</parameter>
1011
<parameter key="monolog.handler.group.class">Monolog\Handler\GroupHandler</parameter>
1112
<parameter key="monolog.handler.buffer.class">Monolog\Handler\BufferHandler</parameter>

‎Resources/config/schema/monolog-1.0.xsd

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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" />
2020
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
21+
<xsd:element name="publisher" type="publisher" minOccurs="0" maxOccurs="1" />
2122
</xsd:sequence>
2223
<xsd:attribute name="type" type="xsd:string" />
2324
<xsd:attribute name="priority" type="xsd:integer" />
@@ -32,7 +33,6 @@
3233
<xsd:attribute name="buffer-size" type="xsd:integer" />
3334
<xsd:attribute name="max-files" type="xsd:integer" />
3435
<xsd:attribute name="handler" type="xsd:string" />
35-
<xsd:attribute name="publisher" type="xsd:string" />
3636
<xsd:attribute name="from-email" type="xsd:string" />
3737
<xsd:attribute name="to-email" type="xsd:string" />
3838
<xsd:attribute name="subject" type="xsd:string" />
@@ -64,6 +64,13 @@
6464
</xsd:restriction>
6565
</xsd:simpleType>
6666

67+
<xsd:complexType name="publisher">
68+
<xsd:attribute name="id" type="xsd:string" />
69+
<xsd:attribute name="hostname" type="xsd:string" />
70+
<xsd:attribute name="port" type="xsd:integer" />
71+
<xsd:attribute name="chunk_size" type="xsd:integer" />
72+
</xsd:complexType>
73+
6774
<xsd:complexType name="email-prototype">
6875
<xsd:attribute name="id" type="xsd:string" />
6976
<xsd:attribute name="method" type="xsd:string" />

‎Tests/DependencyInjection/ConfigurationTest.php

+41-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public function testProcessSimpleCase()
3232

3333
$this->assertArrayHasKey('handlers', $config);
3434
$this->assertArrayHasKey('foobar', $config['handlers']);
35-
$this->assertEquals('stream', $config['handlers']['foobar']['type']);
35+
$this->assertEquals('stream', $config['handlers']['foobar']['type']);
3636
$this->assertEquals('/foo/bar', $config['handlers']['foobar']['path']);
3737
}
3838

3939
public function provideProcessStringChannels()
4040
{
4141
return array(
42-
array('foo', 'foo', true),
42+
array('foo', 'foo', true),
4343
array('!foo', 'foo', false)
4444
);
4545
}
@@ -53,8 +53,8 @@ public function testProcessStringChannels($string, $expectedString, $isInclusive
5353
array(
5454
'handlers' => array(
5555
'foobar' => array(
56-
'type' => 'stream',
57-
'path' => '/foo/bar',
56+
'type' => 'stream',
57+
'path' => '/foo/bar',
5858
'channels' => $string
5959
)
6060
)
@@ -68,6 +68,43 @@ public function testProcessStringChannels($string, $expectedString, $isInclusive
6868
$this->assertEquals($expectedString, $config['handlers']['foobar']['channels']['elements'][0]);
6969
}
7070

71+
public function provideGelfPublisher()
72+
{
73+
return array(
74+
array(
75+
'gelf.publisher'
76+
),
77+
array(
78+
array(
79+
'id' => 'gelf.publisher'
80+
)
81+
)
82+
);
83+
}
84+
85+
/**
86+
* @dataProvider provideGelfPublisher
87+
*/
88+
public function testGelfPublisherService($publisher)
89+
{
90+
$configs = array(
91+
array(
92+
'handlers' => array(
93+
'gelf' => array(
94+
'type' => 'gelf',
95+
'publisher' => $publisher,
96+
),
97+
)
98+
)
99+
);
100+
101+
$config = $this->process($configs);
102+
103+
$this->assertArrayHasKey('id', $config['handlers']['gelf']['publisher']);
104+
$this->assertArrayNotHasKey('hostname', $config['handlers']['gelf']['publisher']);
105+
$this->assertEquals('gelf.publisher', $config['handlers']['gelf']['publisher']['id']);
106+
}
107+
71108
public function testArrays()
72109
{
73110
$configs = array(

‎Tests/DependencyInjection/MonologExtensionTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,28 @@ public function testExceptionWhenUsingBufferWithoutHandler()
194194
$loader->load(array(array('handlers' => array('main' => array('type' => 'buffer')))), $container);
195195
}
196196

197+
/**
198+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
199+
*/
200+
public function testExceptionWhenUsingGelfWithoutPublisher()
201+
{
202+
$container = new ContainerBuilder();
203+
$loader = new MonologExtension();
204+
205+
$loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf')))), $container);
206+
}
207+
208+
/**
209+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
210+
*/
211+
public function testExceptionWhenUsingGelfWithoutPublisherHostname()
212+
{
213+
$container = new ContainerBuilder();
214+
$loader = new MonologExtension();
215+
216+
$loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf', 'publisher' => array())))), $container);
217+
}
218+
197219
/**
198220
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
199221
*/

0 commit comments

Comments
 (0)
Please sign in to comment.