Skip to content

Commit f51d377

Browse files
committed
feature #305 Added support for Redis configuration (Ruud Van den Dungen, lyrixx)
This PR was merged into the 3.x-dev branch. Discussion ---------- Added support for Redis configuration fixed #292 Commits ------- 9667b1f Fixed previous commit (CS) 4fb28f7 Added support for Redis configuration
2 parents 036cd4b + 9667b1f commit f51d377

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing
88
* Fixed regression when `app` channel is defined explicitly
99
* Fixed handlers marked as nested not being ignored properly from the stack
10+
* Added support for Redis configuration
1011

1112
## 3.3.1 (2018-11-04)
1213

DependencyInjection/Configuration.php

+61
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@
8888
* - [level]: level name or int value, defaults to DEBUG
8989
* - [bubble]: bool, defaults to true
9090
*
91+
* - redis:
92+
* - redis:
93+
* - id: optional if host is given
94+
* - host: 127.0.0.1
95+
* - password: null
96+
* - port: 6379
97+
* - database: 0
98+
* - key_name: monolog_redis
99+
*
100+
* - predis:
101+
* - redis:
102+
* - id: optional if host is given
103+
* - host: tcp://10.0.0.1:6379
104+
* - key_name: monolog_redis
105+
*
91106
* - fingers_crossed:
92107
* - handler: the wrapped handler's name
93108
* - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING
@@ -522,6 +537,44 @@ public function getConfigTreeBuilder()
522537
->scalarNode('index')->defaultValue('monolog')->end() // elasticsearch
523538
->scalarNode('document_type')->defaultValue('logs')->end() // elasticsearch
524539
->scalarNode('ignore_error')->defaultValue(false)->end() // elasticsearch
540+
->arrayNode('redis')
541+
->canBeUnset()
542+
->beforeNormalization()
543+
->ifString()
544+
->then(function ($v) { return array('id' => $v); })
545+
->end()
546+
->children()
547+
->scalarNode('id')->end()
548+
->scalarNode('host')->end()
549+
->scalarNode('password')->defaultNull()->end()
550+
->scalarNode('port')->defaultValue(6379)->end()
551+
->scalarNode('database')->defaultValue(0)->end()
552+
->scalarNode('key_name')->defaultValue('monolog_redis')->end()
553+
->end()
554+
->validate()
555+
->ifTrue(function ($v) {
556+
return !isset($v['id']) && !isset($v['host']);
557+
})
558+
->thenInvalid('What must be set is either the host or the service id of the Redis client.')
559+
->end()
560+
->end() // redis
561+
->arrayNode('predis')
562+
->canBeUnset()
563+
->beforeNormalization()
564+
->ifString()
565+
->then(function ($v) { return array('id' => $v); })
566+
->end()
567+
->children()
568+
->scalarNode('id')->end()
569+
->scalarNode('host')->end()
570+
->end()
571+
->validate()
572+
->ifTrue(function ($v) {
573+
return !isset($v['id']) && !isset($v['host']);
574+
})
575+
->thenInvalid('What must be set is either the host or the service id of the Predis client.')
576+
->end()
577+
->end() // predis
525578
->arrayNode('config')
526579
->canBeUnset()
527580
->prototype('scalar')->end()
@@ -849,6 +902,14 @@ public function getConfigTreeBuilder()
849902
->ifTrue(function ($v) { return 'server_log' === $v['type'] && empty($v['host']); })
850903
->thenInvalid('The host has to be specified to use a ServerLogHandler')
851904
->end()
905+
->validate()
906+
->ifTrue(function ($v) { return 'redis' === $v['type'] && empty($v['redis']); })
907+
->thenInvalid('The host has to be specified to use a RedisLogHandler')
908+
->end()
909+
->validate()
910+
->ifTrue(function ($v) { return 'predis' === $v['type'] && empty($v['redis']); })
911+
->thenInvalid('The host has to be specified to use a RedisLogHandler')
912+
->end()
852913
->end()
853914
->validate()
854915
->ifTrue(function ($v) { return isset($v['debug']); })

DependencyInjection/MonologExtension.php

+39
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,43 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
311311
$handler['bubble'],
312312
));
313313
break;
314+
case 'redis':
315+
case 'predis':
316+
if (isset($handler['redis']['id'])) {
317+
$clientId = $handler['redis']['id'];
318+
} elseif ('redis' === $handler['type']) {
319+
if (!class_exists(\Redis::class)) {
320+
throw new \RuntimeException('The \Redis class is not available.');
321+
}
322+
323+
$client = new Definition(\Redis::class);
324+
$client->addMethodCall('connect', array($handler['redis']['host'], $handler['redis']['port']));
325+
$client->addMethodCall('auth', array($handler['redis']['password']));
326+
$client->addMethodCall('select', array($handler['redis']['database']));
327+
$client->setPublic(false);
328+
$clientId = uniqid('monolog.redis.client.', true);
329+
$container->setDefinition($clientId, $client);
330+
} else {
331+
if (!class_exists(\Predis\Client::class)) {
332+
throw new \RuntimeException('The \Predis\Client class is not available.');
333+
}
334+
335+
$client = new Definition(\Predis\Client::class);
336+
$client->setArguments(array(
337+
$handler['redis']['host'],
338+
));
339+
$client->setPublic(false);
340+
341+
$clientId = uniqid('monolog.predis.client.', true);
342+
$container->setDefinition($clientId, $client);
343+
}
344+
$definition->setArguments(array(
345+
new Reference($clientId),
346+
$handler['redis']['key_name'],
347+
$handler['level'],
348+
$handler['bubble'],
349+
));
350+
break;
314351

315352
case 'chromephp':
316353
$definition->setArguments(array(
@@ -835,6 +872,8 @@ private function getHandlerClassByType($handlerType)
835872
'mongo' => 'Monolog\Handler\MongoDBHandler',
836873
'elasticsearch' => 'Monolog\Handler\ElasticSearchHandler',
837874
'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler',
875+
'redis' => 'Monolog\Handler\RedisHandler',
876+
'predis' => 'Monolog\Handler\RedisHandler',
838877
);
839878

840879
if (!isset($typeToClassMapping[$handlerType])) {

Resources/config/schema/monolog-1.0.xsd

+14
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@
157157
<xsd:attribute name="collection" type="xsd:string" />
158158
</xsd:complexType>
159159

160+
<xsd:complexType name="redis">
161+
<xsd:attribute name="id" type="xsd:string" />
162+
<xsd:attribute name="host" type="xsd:string" />
163+
<xsd:attribute name="password" type="xsd:string" />
164+
<xsd:attribute name="port" type="xsd:integer" />
165+
<xsd:attribute name="database" type="xsd:integer" />
166+
<xsd:attribute name="key_name" type="xsd:string" />
167+
</xsd:complexType>
168+
169+
<xsd:complexType name="predis">
170+
<xsd:attribute name="id" type="xsd:string" />
171+
<xsd:attribute name="host" type="xsd:string" />
172+
</xsd:complexType>
173+
160174
<xsd:complexType name="elasticsearch">
161175
<xsd:attribute name="id" type="xsd:string" />
162176
<xsd:attribute name="host" type="xsd:string" />

Tests/DependencyInjection/ConfigurationTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,50 @@ public function testWithNestedHandler()
362362

363363
$this->assertTrue($config['handlers']['foobar']['nested']);
364364
}
365+
public function testWithRedisHandler()
366+
{
367+
$configs = array(
368+
array(
369+
'handlers' => array(
370+
'redis' => array(
371+
'type' => 'redis',
372+
'redis' => array(
373+
'host' => '127.0.1.1',
374+
'password' => 'pa$$w0rd',
375+
'port' => 1234,
376+
'database' => 1,
377+
'key_name' => 'monolog_redis_test'
378+
)
379+
)
380+
)
381+
)
382+
);
383+
$config = $this->process($configs);
384+
385+
$this->assertEquals('127.0.1.1', $config['handlers']['redis']['redis']['host']);
386+
$this->assertEquals('pa$$w0rd', $config['handlers']['redis']['redis']['password']);
387+
$this->assertEquals(1234, $config['handlers']['redis']['redis']['port']);
388+
$this->assertEquals(1, $config['handlers']['redis']['redis']['database']);
389+
$this->assertEquals('monolog_redis_test', $config['handlers']['redis']['redis']['key_name']);
390+
391+
$configs = array(
392+
array(
393+
'handlers' => array(
394+
'redis' => array(
395+
'type' => 'predis',
396+
'redis' => array(
397+
'host' => '127.0.1.1',
398+
'key_name' => 'monolog_redis_test'
399+
)
400+
)
401+
)
402+
)
403+
);
404+
$config = $this->process($configs);
405+
406+
$this->assertEquals('127.0.1.1', $config['handlers']['redis']['redis']['host']);
407+
$this->assertEquals('monolog_redis_test', $config['handlers']['redis']['redis']['key_name']);
408+
}
365409

366410
/**
367411
* Processes an array of configurations and returns a compiled version.

0 commit comments

Comments
 (0)