From 27143f3b84a77c423d13042f8cada844ce46549c Mon Sep 17 00:00:00 2001 From: Argyrios Gounaris Date: Wed, 19 Nov 2014 16:28:51 +0000 Subject: [PATCH 1/2] adding support for elasticsearch --- DependencyInjection/Configuration.php | 15 ++++++++++ DependencyInjection/MonologExtension.php | 38 ++++++++++++++++++++++++ Resources/config/monolog.xml | 7 +++++ Resources/config/schema/monolog-1.0.xsd | 7 +++++ 4 files changed, 67 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ee689747..728ed858 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -374,6 +374,21 @@ public function getConfigTreeBuilder() ->thenInvalid('If you set user, you must provide a password.') ->end() ->end() // mongo + ->arrayNode('elasticsearch') + ->canBeUnset() + ->children() + ->scalarNode('host')->end() + ->scalarNode('port')->defaultValue(9200)->end() + ->scalarNode('index')->defaultValue('monolog')->end() + ->scalarNode('index_type')->defaultValue('logs')->end() + ->end() + ->validate() + ->ifTrue(function ($v) { + return !isset($v['host']); + }) + ->thenInvalid('What must be set is host.') + ->end() + ->end() // elasticsearch ->arrayNode('config') ->canBeUnset() ->prototype('scalar')->end() diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index d50efc6d..ae31d714 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -11,6 +11,8 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection; +use Monolog\Formatter\ElasticaFormatter; +use Monolog\Logger; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -18,6 +20,7 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerInterface; +use Elastica\Client; /** * MonologExtension is an extension for the Monolog library. @@ -229,6 +232,41 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler )); break; + case 'elasticsearch': + // elastica client new definition + $elasticaClient = new Definition('%monolog.elastica.client.class%'); + $elasticaClient->setArguments(array( + array( + 'host' => $handler['elasticsearch']['host'], + 'port' => $handler['elasticsearch']['port'] + ) + ) + ); + $container->setDefinition('monolog.elastica.client', $elasticaClient); + + // set parameters for monolog.formatter.elastica service check monolog.xml + $container->setParameter('monolog.elasticsearch.index', $handler['elasticsearch']['index']); + $container->setParameter('monolog.elasticsearch.index_type', $handler['elasticsearch']['index_type']); + + // apply tags in case we want to use channels + if (!empty($handler['tags'])) { + foreach ($handler['tags'] as $tag) { + $definition->addTag($tag); + } + } + + // elastica handler definition + $definition->setArguments(array( + new Reference('monolog.elastica.client'), + array( + 'index' => $handler['elasticsearch']['index'], + 'type' => $handler['elasticsearch']['index_type'], + ), + $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 9ee5414c..b948e386 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -44,6 +44,9 @@ Monolog\Handler\FilterHandler Monolog\Handler\MongoDBHandler MongoClient + + Monolog\Handler\ElasticSearchHandler + Elastica\Client @@ -67,5 +70,9 @@ + + %monolog.elasticsearch.index% + %monolog.elasticsearch.index_type% + diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index ac841eed..cb211d8b 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -131,4 +131,11 @@ + + + + + + + From 8a2a6bd98754eda20b4572022093a1003f08d0e5 Mon Sep 17 00:00:00 2001 From: Argyrios Gounaris Date: Wed, 19 Nov 2014 16:48:45 +0000 Subject: [PATCH 2/2] adding configuration documentation --- DependencyInjection/Configuration.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 728ed858..f2907f9b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -71,6 +71,15 @@ * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true * + * - elasticsearch: + * - elasticsearch: + * - host: elastic search host name + * - [port]: defaults to 9200 + * - [index]: index name, defaults to monolog + * - [index_type]: index_type name, 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