Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedisHandler support #262

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@
* - host: server log host. ex: 127.0.0.1:9911
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - redis:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add id here

* - id: optional if host is given
* - host: 127.0.0.1
* - password: null
* - port: 6379
* - database: 0
* - key_name: monolog_redis
*
* - predis:
* - id: optional if host is given
* - host: tcp://10.0.0.1:6379
* - key_name: monolog_redis
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Christophe Coevoet <stof@notk.org>
Expand Down Expand Up @@ -506,6 +519,44 @@ public function getConfigTreeBuilder()
->thenInvalid('What must be set is either the host or the id.')
->end()
->end() // elasticsearch
->arrayNode('redis')
->canBeUnset()
->beforeNormalization()
->ifString()
->then(function ($v) { return array('id' => $v); })
->end()
->children()
->scalarNode('id')->end()
->scalarNode('host')->end()
->scalarNode('password')->defaultNull()->end()
->scalarNode('port')->defaultValue(6379)->end()
->scalarNode('database')->defaultValue(0)->end()
->scalarNode('key_name')->defaultValue('monolog_redis')->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() // redis
->arrayNode('predis')
->canBeUnset()
->beforeNormalization()
->ifString()
->then(function ($v) { return array('id' => $v); })
->end()
->children()
->scalarNode('id')->end()
->scalarNode('host')->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() // predis
->scalarNode('index')->defaultValue('monolog')->end() // elasticsearch
->scalarNode('document_type')->defaultValue('logs')->end() // elasticsearch
->scalarNode('ignore_error')->defaultValue(false)->end() // elasticsearch
Expand Down Expand Up @@ -821,6 +872,14 @@ public function getConfigTreeBuilder()
->ifTrue(function ($v) { return 'server_log' === $v['type'] && empty($v['host']); })
->thenInvalid('The host has to be specified to use a ServerLogHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'redis' === $v['type'] && empty($v['redis']); })
->thenInvalid('The host has to be specified to use a RedisLogHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'predis' === $v['type'] && empty($v['redis']); })
->thenInvalid('The host has to be specified to use a RedisLogHandler')
->end()
->end()
->validate()
->ifTrue(function ($v) { return isset($v['debug']); })
Expand Down
42 changes: 42 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,46 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
));
break;

case 'redis':
case 'predis':
if (isset($handler['redis']['id'])) {
$clientId = $handler['redis']['id'];
}elseif('redis' === $handler['type']){
if(class_exists('\Redis')){
$client = new Definition('\Redis');
$client->addMethodCall('connect', array($handler['redis']['host'], $handler['redis']['port']));
$client->addMethodCall('auth', array($handler['redis']['password']));
$client->addMethodCall('select', array($handler['redis']['database']));
$client->setPublic(false);

$clientId = uniqid('monolog.redis.client.', true);
$container->setDefinition($clientId, $client);
}else{
throw new \RuntimeException('The \Redis is not available.');
}
}else{
if(class_exists('\Predis\Client')){
$client = new Definition('\Predis\Client');
$client->setArguments(array(
$handler['redis']['host'],
));
$client->setPublic(false);

$clientId = uniqid('monolog.predis.client.', true);
$container->setDefinition($clientId, $client);
}else{
throw new \RuntimeException('The \Predis\Client is not available.');
}
}

$definition->setArguments(array(
new Reference($clientId),
$handler['redis']['key_name'],
$handler['level'],
$handler['bubble'],
));
break;

default:
throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"', $handler['type'], $name));
}
Expand Down Expand Up @@ -770,6 +810,8 @@ private function getHandlerClassByType($handlerType)
'mongo' => 'Monolog\Handler\MongoDBHandler',
'elasticsearch' => 'Monolog\Handler\ElasticSearchHandler',
'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler',
'redis' => 'Monolog\Handler\RedisHandler',
'predis' => 'Monolog\Handler\RedisHandler',
);

if (!isset($typeToClassMapping[$handlerType])) {
Expand Down
20 changes: 17 additions & 3 deletions Resources/config/schema/monolog-1.0.xsd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>

<xsd:schema xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://symfony.com/schema/dic/monolog"
elementFormDefault="qualified">
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://symfony.com/schema/dic/monolog"
elementFormDefault="qualified">

<xsd:element name="config" type="config" />

Expand Down Expand Up @@ -171,4 +171,18 @@
</xsd:choice>
<xsd:attribute name="code" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="redis">
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="password" type="xsd:string" />
<xsd:attribute name="port" type="xsd:integer" />
<xsd:attribute name="database" type="xsd:integer" />
<xsd:attribute name="key_name" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="predis">
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
47 changes: 47 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,53 @@ public function testWithNestedHandler()

$this->assertTrue($config['handlers']['foobar']['nested']);
}

public function testWithRedisHandler()
{
$configs = array(
array(
'handlers' => array(
'redis' => array(
'type' => 'redis',
'redis' => array(
'host' => '127.0.1.1',
'password' => 'pa$$w0rd',
'port' => 1234,
'database' => 1,
'key_name' => 'monolog_redis_test'
)
)
)
)
);

$config = $this->process($configs);

$this->assertEquals('127.0.1.1', $config['handlers']['redis']['redis']['host']);
$this->assertEquals('pa$$w0rd', $config['handlers']['redis']['redis']['password']);
$this->assertEquals(1234, $config['handlers']['redis']['redis']['port']);
$this->assertEquals(1, $config['handlers']['redis']['redis']['database']);
$this->assertEquals('monolog_redis_test', $config['handlers']['redis']['redis']['key_name']);

$configs = array(
array(
'handlers' => array(
'redis' => array(
'type' => 'predis',
'redis' => array(
'host' => '127.0.1.1',
'key_name' => 'monolog_redis_test'
)
)
)
)
);

$config = $this->process($configs);

$this->assertEquals('127.0.1.1', $config['handlers']['redis']['redis']['host']);
$this->assertEquals('monolog_redis_test', $config['handlers']['redis']['redis']['key_name']);
}

/**
* Processes an array of configurations and returns a compiled version.
Expand Down