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

Add support to log to redis #292

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
00ed2f3
Add stream handler lock option
mRoca Dec 7, 2017
9f6f0b6
Allow setting "ident" parameter for SyslogUdpHandler, affects #266
dhirtzbruch May 5, 2018
86fd51e
Support environment option for raven client
Taluu Jun 12, 2018
a3790d9
add support for redis
Oct 15, 2018
7fdda67
Register processors for autoconfiguration
nicolas-grekas Nov 10, 2018
d24abd1
Merge branch 'master' of https://github.com/symfony/monolog-bundle
Jan 3, 2019
24291fb
Remove duplicate entry
B-Galati Jan 16, 2019
523768f
minor #293 Remove duplicate entry (B-Galati)
lyrixx Jan 31, 2019
cc9abf6
Expose configuration for the ConsoleHandler
lyrixx Feb 22, 2019
005aefb
feature #297 Expose configuration for the ConsoleHandler (lyrixx)
lyrixx Mar 12, 2019
981d74f
Fix typo in PhpDoc
jdreesen May 2, 2019
969e476
feature #285 Register processors for autoconfiguration (nicolas-grekas)
lyrixx May 16, 2019
962a2e1
minor #301 Fix typo in PhpDoc (jdreesen)
lyrixx May 16, 2019
366f692
Flush loggers on kernel.reset
dnna Sep 9, 2018
6684794
feature #279 Flush loggers on kernel.reset (dnna)
lyrixx May 16, 2019
a7c1046
Fixed CS and CHANGELOG
lyrixx May 16, 2019
f6c8593
Deprecrate "excluded_404s" option
geoffrey-brier Jul 26, 2018
d810fcc
feature #302 Deprecate "excluded_404s" option (geoffrey-brier)
lyrixx May 16, 2019
9f8e531
Fixed PHPUnit Warning
lyrixx May 16, 2019
c720aab
feature #270 Support environment option for raven client (Taluu)
lyrixx May 16, 2019
b97e237
feature #267 Allow setting "ident" parameter for SyslogUdpHandler, af…
lyrixx May 16, 2019
a6c3267
feature #303 Fixed PHPUnit Warning (lyrixx)
lyrixx May 16, 2019
036cd4b
feature #242 Add stream handler lock option (mRoca)
lyrixx May 16, 2019
2e0714e
add better explanation of wat an id is
May 28, 2019
100cfde
fix handler key not correct
May 28, 2019
a143416
add support for redis
Oct 15, 2018
f07d51b
add better explanation of wat an id is
May 28, 2019
9c992e1
fix handler key not correct
May 28, 2019
efc4265
Merge branch 'add-redis-config-support' of https://github.com/ruudvdd…
Jun 3, 2019
b43daff
fix exception messages when a certain redis class does not exist
Jun 3, 2019
7a1dcab
add changelog rule
Jun 3, 2019
56709bc
add enter between types
Jun 3, 2019
e2fc27d
change back type and fix configuration
Jun 3, 2019
5448621
add support for redis
Oct 15, 2018
4a01df8
add better explanation of wat an id is
May 28, 2019
eabdb9c
fix handler key not correct
May 28, 2019
ad6b37f
add support for redis
Oct 15, 2018
e23d1ff
add better explanation of wat an id is
May 28, 2019
d1ca48b
fix handler key not correct
May 28, 2019
2e09bfd
fix exception messages when a certain redis class does not exist
Jun 3, 2019
5d8ee96
add changelog rule
Jun 3, 2019
ec0dc0c
add enter between types
Jun 3, 2019
7a012af
change back type and fix configuration
Jun 3, 2019
dee6679
Merge branch 'add-redis-config-support' of https://github.com/ruudvdd…
Jun 3, 2019
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
58 changes: 58 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -85,6 +85,18 @@
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - redis:
* - 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
* - fingers_crossed:
* - handler: the wrapped handler's name
* - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING
Expand Down Expand Up @@ -517,6 +529,44 @@ public function getConfigTreeBuilder()
->scalarNode('index')->defaultValue('monolog')->end() // elasticsearch
->scalarNode('document_type')->defaultValue('logs')->end() // elasticsearch
->scalarNode('ignore_error')->defaultValue(false)->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 service id of the Redis client.')
->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 service id of the Predis client.')
->end()
->end() // predis
->arrayNode('config')
->canBeUnset()
->prototype('scalar')->end()
Expand Down Expand Up @@ -833,6 +883,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
41 changes: 41 additions & 0 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -291,6 +291,45 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$handler['bubble'],
));
break;
case 'redis':
case 'predis':
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
$type = $handler['type'];

if (isset($handler[$type]['id'])) {
$clientId = $handler[$type]['id'];
} elseif ('redis' === $type) {
if (!class_exists('\Redis')) {
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
throw new \RuntimeException('The \Redis is not available.');
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
}

$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 {
if (!class_exists('\Predis\Client')) {
throw new \RuntimeException('The \Predis\Client is not available.');
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
}

$client = new Definition('\Predis\Client');
$client->setArguments(array(
$handler['predis']['host'],
));
$client->setPublic(false);

$clientId = uniqid('monolog.predis.client.', true);
$container->setDefinition($clientId, $client);
}
$definition->setArguments(array(
new Reference($clientId),
$handler[$type]['key_name'],
$handler['level'],
$handler['bubble'],
));
break;

case 'chromephp':
$definition->setArguments(array(
Expand Down Expand Up @@ -801,6 +840,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
14 changes: 14 additions & 0 deletions Resources/config/schema/monolog-1.0.xsd
Expand Up @@ -156,6 +156,20 @@
<xsd:attribute name="collection" type="xsd:string" />
</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:complexType name="elasticsearch">
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
Expand Down
44 changes: 44 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -337,6 +337,50 @@ 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