diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7207d0a0..8b7ea081 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -55,6 +55,18 @@ * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true * + * - mongo: + * - mongo: + * - id: optional if host is given + * - host: database host name, optional if id is given + * - [port]: defaults to 27017 + * - [user]: database user name + * - pass: mandatory only if user is present + * - [database]: defaults to monolog + * - [collection]: defaults to logs + * - [level]: level name or int value, defaults to DEBUG + * - [bubble]: bool, defaults to true + * * - fingers_crossed: * - handler: the wrapped handler's name * - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING @@ -247,6 +259,34 @@ public function getConfigTreeBuilder() ->thenInvalid('What must be set is either the hostname or the id.') ->end() ->end() // gelf + ->arrayNode('mongo') + ->canBeUnset() + ->beforeNormalization() + ->ifString() + ->then(function($v) { return array('id'=> $v); }) + ->end() + ->children() + ->scalarNode('id')->end() + ->scalarNode('host')->end() + ->scalarNode('port')->defaultValue(27017)->end() + ->scalarNode('user')->end() + ->scalarNode('pass')->end() + ->scalarNode('database')->defaultValue('monolog')->end() + ->scalarNode('collection')->defaultValue('logs')->end() + ->end() + ->validate() + ->ifTrue(function($v) { + return !isset($v['id']) && !isset($v['host']); + }) + ->thenInvalid('What must be set is either the host or the id.') + ->end() + ->validate() + ->ifTrue(function($v) { + return isset($v['user']) && !isset($v['pass']); + }) + ->thenInvalid('If you set user, you must provide a password.') + ->end() + ->end() // mongo ->arrayNode('members') // group ->canBeUnset() ->performNoDeepMerging() @@ -440,6 +480,10 @@ public function getConfigTreeBuilder() ->ifTrue(function($v) { return 'cube' === $v['type'] && empty($v['url']); }) ->thenInvalid('The url has to be specified to use a CubeHandler') ->end() + ->validate() + ->ifTrue(function($v) { return 'mongo' === $v['type'] && !isset($v['mongo']); }) + ->thenInvalid('The mongo configuration has to be specified to use a MongoHandler') + ->end() ->validate() ->ifTrue(function($v) { return 'amqp' === $v['type'] && empty($v['exchange']); }) ->thenInvalid('The exchange has to be specified to use a AmqpHandler') diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index d1771d46..09366c56 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -168,6 +168,37 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler )); break; + case 'mongo': + + if (isset($handler['mongo']['id'])) { + $clientId = $handler['mongo']['id']; + } else { + $server = 'mongodb://'; + + if(isset($handler['mongo']['user'])) { + $server .= $handler['mongo']['user'] . ':' . $handler['mongo']['pass'] . '@'; + } + + $server .= $handler['mongo']['host'] . ':' . $handler['mongo']['port']; + + $client = new Definition("%monolog.mongo.client.class%", array( + $server + )); + + $clientId = uniqid('monolog.mongo.client.'); + $client->setPublic(false); + $container->setDefinition($clientId, $client); + } + + $definition->setArguments(array( + new Reference($clientId), + $handler['mongo']['database'], + $handler['mongo']['collection'], + $handler['level'], + $handler['bubble'], + )); + break; + case 'chromephp': $definition->setArguments(array( $handler['level'], diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index 268f5c87..1e5c2119 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -33,6 +33,9 @@ Monolog\Handler\FingersCrossedHandler Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy + Monolog\Handler\MongoDBHandler + MongoClient + diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index ca8ebb34..84b6162a 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -19,6 +19,7 @@ + @@ -89,4 +90,15 @@ + + + + + + + + + + +