Skip to content

Commit 2555f14

Browse files
committed
Adjust elasticsearch configuration, refs #108
1 parent fb6c492 commit 2555f14

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

DependencyInjection/Configuration.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@
7373
*
7474
* - elasticsearch:
7575
* - elasticsearch:
76+
* - id: optional if host is given
7677
* - host: elastic search host name
7778
* - [port]: defaults to 9200
78-
* - [index]: index name, defaults to monolog
79-
* - [index_type]: index_type name, defaults to logs
79+
* - [index]: index name, defaults to monolog
80+
* - [document_type]: document_type, defaults to logs
8081
* - [level]: level name or int value, defaults to DEBUG
8182
* - [bubble]: bool, defaults to true
8283
*
@@ -385,19 +386,24 @@ public function getConfigTreeBuilder()
385386
->end() // mongo
386387
->arrayNode('elasticsearch')
387388
->canBeUnset()
389+
->beforeNormalization()
390+
->ifString()
391+
->then(function ($v) { return array('id'=> $v); })
392+
->end()
388393
->children()
394+
->scalarNode('id')->end()
389395
->scalarNode('host')->end()
390396
->scalarNode('port')->defaultValue(9200)->end()
391-
->scalarNode('index')->defaultValue('monolog')->end()
392-
->scalarNode('index_type')->defaultValue('logs')->end()
393397
->end()
394398
->validate()
395399
->ifTrue(function ($v) {
396-
return !isset($v['host']);
400+
return !isset($v['id']) && !isset($v['host']);
397401
})
398-
->thenInvalid('What must be set is host.')
402+
->thenInvalid('What must be set is either the host or the id.')
399403
->end()
400404
->end() // elasticsearch
405+
->scalarNode('index')->defaultValue('monolog')->end() // elasticsearch
406+
->scalarNode('document_type')->defaultValue('logs')->end() // elasticsearch
401407
->arrayNode('config')
402408
->canBeUnset()
403409
->prototype('scalar')->end()

DependencyInjection/MonologExtension.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,34 +233,29 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
233233
break;
234234

235235
case 'elasticsearch':
236-
// elastica client new definition
237-
$elasticaClient = new Definition('%monolog.elastica.client.class%');
238-
$elasticaClient->setArguments(array(
236+
if (isset($handler['elasticsearch']['id'])) {
237+
$clientId = $handler['elasticsearch']['id'];
238+
} else {
239+
// elastica client new definition
240+
$elasticaClient = new Definition('%monolog.elastica.client.class%');
241+
$elasticaClient->setArguments(array(
239242
array(
240243
'host' => $handler['elasticsearch']['host'],
241244
'port' => $handler['elasticsearch']['port']
242245
)
243-
)
244-
);
245-
$container->setDefinition('monolog.elastica.client', $elasticaClient);
246-
247-
// set parameters for monolog.formatter.elastica service check monolog.xml
248-
$container->setParameter('monolog.elasticsearch.index', $handler['elasticsearch']['index']);
249-
$container->setParameter('monolog.elasticsearch.index_type', $handler['elasticsearch']['index_type']);
246+
));
250247

251-
// apply tags in case we want to use channels
252-
if (!empty($handler['tags'])) {
253-
foreach ($handler['tags'] as $tag) {
254-
$definition->addTag($tag);
255-
}
248+
$clientId = uniqid('monolog.elastica.client.');
249+
$elasticaClient->setPublic(false);
250+
$container->setDefinition($clientId, $elasticaClient);
256251
}
257252

258253
// elastica handler definition
259254
$definition->setArguments(array(
260-
new Reference('monolog.elastica.client'),
255+
new Reference($clientId),
261256
array(
262-
'index' => $handler['elasticsearch']['index'],
263-
'type' => $handler['elasticsearch']['index_type'],
257+
'index' => $handler['index'],
258+
'type' => $handler['document_type'],
264259
),
265260
$handler['level'],
266261
$handler['bubble']

Resources/config/monolog.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,5 @@
7070
<service id="monolog.formatter.normalizer" class="Monolog\Formatter\NormalizerFormatter" public="false" />
7171
<service id="monolog.formatter.scalar" class="Monolog\Formatter\ScalarFormatter" public="false" />
7272
<service id="monolog.formatter.wildfire" class="Monolog\Formatter\WildfireFormatter" public="false" />
73-
<service id="monolog.formatter.elastica" class="Monolog\Formatter\ElasticaFormatter" public="false">
74-
<argument index="0">%monolog.elasticsearch.index%</argument>
75-
<argument index="1">%monolog.elasticsearch.index_type%</argument>
76-
</service>
7773
</services>
7874
</container>

Resources/config/schema/monolog-1.0.xsd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
2222
<xsd:element name="publisher" type="publisher" minOccurs="0" maxOccurs="1" />
2323
<xsd:element name="mongo" type="mongo" minOccurs="0" maxOccurs="1" />
24+
<xsd:element name="elasticsearch" type="elasticsearch" minOccurs="0" maxOccurs="1" />
2425
<xsd:element name="config" type="xsd:anyType" minOccurs="0" maxOccurs="1" />
2526
<xsd:element name="excluded-404" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
2627
<xsd:element name="tag" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -62,6 +63,8 @@
6263
<xsd:attribute name="use-attachment" type="xsd:boolean" />
6364
<xsd:attribute name="icon-emoji" type="xsd:string" />
6465
<xsd:attribute name="file-permission" type="xsd:string" />
66+
<xsd:attribute name="index" type="xsd:string" />
67+
<xsd:attribute name="document_type" type="xsd:string" />
6568
</xsd:complexType>
6669

6770
<xsd:simpleType name="level">
@@ -132,10 +135,8 @@
132135
</xsd:complexType>
133136

134137
<xsd:complexType name="elasticsearch">
138+
<xsd:attribute name="id" type="xsd:string" />
135139
<xsd:attribute name="host" type="xsd:string" />
136140
<xsd:attribute name="port" type="xsd:integer" />
137-
<xsd:attribute name="index" type="xsd:string" />
138-
<xsd:attribute name="index_type" type="xsd:string" />
139141
</xsd:complexType>
140-
141142
</xsd:schema>

0 commit comments

Comments
 (0)