Skip to content

Commit c043ada

Browse files
committed
Merge pull request #55 from pacoVela/master
Basic Mongo implementation
2 parents ed6d095 + 62130dc commit c043ada

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@
5555
* - [level]: level name or int value, defaults to DEBUG
5656
* - [bubble]: bool, defaults to true
5757
*
58+
* - mongo:
59+
* - mongo:
60+
* - id: optional if host is given
61+
* - host: database host name, optional if id is given
62+
* - [port]: defaults to 27017
63+
* - [user]: database user name
64+
* - pass: mandatory only if user is present
65+
* - [database]: defaults to monolog
66+
* - [collection]: defaults to logs
67+
* - [level]: level name or int value, defaults to DEBUG
68+
* - [bubble]: bool, defaults to true
69+
*
5870
* - fingers_crossed:
5971
* - handler: the wrapped handler's name
6072
* - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING
@@ -247,6 +259,34 @@ public function getConfigTreeBuilder()
247259
->thenInvalid('What must be set is either the hostname or the id.')
248260
->end()
249261
->end() // gelf
262+
->arrayNode('mongo')
263+
->canBeUnset()
264+
->beforeNormalization()
265+
->ifString()
266+
->then(function($v) { return array('id'=> $v); })
267+
->end()
268+
->children()
269+
->scalarNode('id')->end()
270+
->scalarNode('host')->end()
271+
->scalarNode('port')->defaultValue(27017)->end()
272+
->scalarNode('user')->end()
273+
->scalarNode('pass')->end()
274+
->scalarNode('database')->defaultValue('monolog')->end()
275+
->scalarNode('collection')->defaultValue('logs')->end()
276+
->end()
277+
->validate()
278+
->ifTrue(function($v) {
279+
return !isset($v['id']) && !isset($v['host']);
280+
})
281+
->thenInvalid('What must be set is either the host or the id.')
282+
->end()
283+
->validate()
284+
->ifTrue(function($v) {
285+
return isset($v['user']) && !isset($v['pass']);
286+
})
287+
->thenInvalid('If you set user, you must provide a password.')
288+
->end()
289+
->end() // mongo
250290
->arrayNode('members') // group
251291
->canBeUnset()
252292
->performNoDeepMerging()
@@ -440,6 +480,10 @@ public function getConfigTreeBuilder()
440480
->ifTrue(function($v) { return 'cube' === $v['type'] && empty($v['url']); })
441481
->thenInvalid('The url has to be specified to use a CubeHandler')
442482
->end()
483+
->validate()
484+
->ifTrue(function($v) { return 'mongo' === $v['type'] && !isset($v['mongo']); })
485+
->thenInvalid('The mongo configuration has to be specified to use a MongoHandler')
486+
->end()
443487
->validate()
444488
->ifTrue(function($v) { return 'amqp' === $v['type'] && empty($v['exchange']); })
445489
->thenInvalid('The exchange has to be specified to use a AmqpHandler')

DependencyInjection/MonologExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,37 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
168168
));
169169
break;
170170

171+
case 'mongo':
172+
173+
if (isset($handler['mongo']['id'])) {
174+
$clientId = $handler['mongo']['id'];
175+
} else {
176+
$server = 'mongodb://';
177+
178+
if(isset($handler['mongo']['user'])) {
179+
$server .= $handler['mongo']['user'] . ':' . $handler['mongo']['pass'] . '@';
180+
}
181+
182+
$server .= $handler['mongo']['host'] . ':' . $handler['mongo']['port'];
183+
184+
$client = new Definition("%monolog.mongo.client.class%", array(
185+
$server
186+
));
187+
188+
$clientId = uniqid('monolog.mongo.client.');
189+
$client->setPublic(false);
190+
$container->setDefinition($clientId, $client);
191+
}
192+
193+
$definition->setArguments(array(
194+
new Reference($clientId),
195+
$handler['mongo']['database'],
196+
$handler['mongo']['collection'],
197+
$handler['level'],
198+
$handler['bubble'],
199+
));
200+
break;
201+
171202
case 'chromephp':
172203
$definition->setArguments(array(
173204
$handler['level'],

Resources/config/monolog.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
<parameter key="monolog.handler.fingers_crossed.class">Monolog\Handler\FingersCrossedHandler</parameter>
3535
<parameter key="monolog.handler.fingers_crossed.error_level_activation_strategy.class">Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy</parameter>
36+
<parameter key="monolog.handler.mongo.class">Monolog\Handler\MongoDBHandler</parameter>
37+
<parameter key="monolog.mongo.client.class">MongoClient</parameter>
38+
3639
</parameters>
3740

3841
<services>

Resources/config/schema/monolog-1.0.xsd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<xsd:element name="member" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
2020
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
2121
<xsd:element name="publisher" type="publisher" minOccurs="0" maxOccurs="1" />
22+
<xsd:element name="mongo" type="mongo" minOccurs="0" maxOccurs="1" />
2223
</xsd:sequence>
2324
<xsd:attribute name="type" type="xsd:string" />
2425
<xsd:attribute name="priority" type="xsd:integer" />
@@ -89,4 +90,15 @@
8990
<xsd:enumeration value="exclusive" />
9091
</xsd:restriction>
9192
</xsd:simpleType>
93+
94+
<xsd:complexType name="mongo">
95+
<xsd:attribute name="id" type="xsd:string" />
96+
<xsd:attribute name="host" type="xsd:string" />
97+
<xsd:attribute name="port" type="xsd:integer" />
98+
<xsd:attribute name="user" type="xsd:string" />
99+
<xsd:attribute name="pass" type="xsd:string" />
100+
<xsd:attribute name="database" type="xsd:string" />
101+
<xsd:attribute name="collection" type="xsd:string" />
102+
</xsd:complexType>
103+
92104
</xsd:schema>

0 commit comments

Comments
 (0)