From d03ca982ef3311cb1ce6bcbbc2371ba39bc1a468 Mon Sep 17 00:00:00 2001 From: Paco Vela Date: Fri, 4 Oct 2013 17:09:31 +0200 Subject: [PATCH 1/5] Basic mongo implementation --- DependencyInjection/Configuration.php | 27 +++++++++++++++++++++ DependencyInjection/MonologExtension.php | 31 ++++++++++++++++++++++++ Resources/config/monolog.xml | 3 +++ Resources/config/schema/monolog-1.0.xsd | 12 +++++++++ 4 files changed, 73 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7207d0a0..c3396530 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -55,6 +55,11 @@ * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true * + * - mongo: + * - mongo: {id: ...} or {host, [port], [user, pass], database, collection} + * - [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 +252,28 @@ 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')->end() + ->scalarNode('collection')->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() + ->end() // mongo ->arrayNode('members') // group ->canBeUnset() ->performNoDeepMerging() diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index d1771d46..15ffcc46 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 = '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 @@ + + + + + + + + + + + From f994fd9b7bc81c6af703dc8fae9ca32fd09960d1 Mon Sep 17 00:00:00 2001 From: Paco Vela Date: Tue, 8 Oct 2013 12:05:14 +0200 Subject: [PATCH 2/5] Param check: if user is set, pass must be present (mongo) --- DependencyInjection/Configuration.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c3396530..2fdec8b5 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -273,6 +273,12 @@ public function getConfigTreeBuilder() }) ->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() From 6dfe1fc3d5f9d75abebe017044c751f499a28ebe Mon Sep 17 00:00:00 2001 From: Paco Vela Date: Tue, 15 Oct 2013 10:14:51 +0200 Subject: [PATCH 3/5] UniqId for service name --- DependencyInjection/MonologExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 15ffcc46..09366c56 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -185,7 +185,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $server )); - $clientId = 'monolog.mongo.client'; + $clientId = uniqid('monolog.mongo.client.'); $client->setPublic(false); $container->setDefinition($clientId, $client); } From d22ca4deb5edb95abac783583bd1f5246fe957e0 Mon Sep 17 00:00:00 2001 From: Paco Vela Date: Tue, 15 Oct 2013 11:50:03 +0200 Subject: [PATCH 4/5] Give default values for mongo database and collection. --- DependencyInjection/Configuration.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2fdec8b5..c7f894c9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -56,7 +56,14 @@ * - [bubble]: bool, defaults to true * * - mongo: - * - mongo: {id: ...} or {host, [port], [user, pass], database, collection} + * - 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 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 * @@ -264,8 +271,8 @@ public function getConfigTreeBuilder() ->scalarNode('port')->defaultValue(27017)->end() ->scalarNode('user')->end() ->scalarNode('pass')->end() - ->scalarNode('database')->end() - ->scalarNode('collection')->end() + ->scalarNode('database')->defaultValue('monolog')->end() + ->scalarNode('collection')->defaultValue('logs')->end() ->end() ->validate() ->ifTrue(function($v) { From 62130dc4970d3f8a732546769be74c315f1d64a7 Mon Sep 17 00:00:00 2001 From: Paco Vela Date: Wed, 16 Oct 2013 09:19:34 +0200 Subject: [PATCH 5/5] The mongo configuration has to be specified to use a MongoHandler --- DependencyInjection/Configuration.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c7f894c9..8b7ea081 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -56,12 +56,12 @@ * - [bubble]: bool, defaults to true * * - mongo: - * - mongo: - * - id: optional if host is given + * - 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 if user is present + * - pass: mandatory only if user is present * - [database]: defaults to monolog * - [collection]: defaults to logs * - [level]: level name or int value, defaults to DEBUG @@ -480,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')