From 00ed2f3c087e74b462f188b228ff1d428c4ed65e Mon Sep 17 00:00:00 2001 From: Michel Roca Date: Thu, 7 Dec 2017 15:26:21 +0100 Subject: [PATCH 01/31] Add stream handler lock option --- DependencyInjection/Configuration.php | 3 +++ DependencyInjection/MonologExtension.php | 1 + Resources/config/schema/monolog-1.0.xsd | 1 + .../DependencyInjection/ConfigurationTest.php | 25 +++++++++++++++++++ .../FixtureMonologExtensionTest.php | 8 +++--- .../MonologExtensionTest.php | 8 +++--- 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e8ccaa48..41ffe76a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -31,6 +31,8 @@ * - path: string * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true + * - [file_permission]: int|null, defaults to null (0644) + * - [use_locking]: bool, defaults to false * * - console: * - [verbosity_levels]: level => verbosity configuration @@ -349,6 +351,7 @@ public function getConfigTreeBuilder() }) ->end() ->end() + ->booleanNode('use_locking')->defaultFalse()->end() // stream ->scalarNode('filename_format')->defaultValue('{filename}-{date}')->end() //rotating ->scalarNode('date_format')->defaultValue('Y-m-d')->end() //rotating ->scalarNode('ident')->defaultFalse()->end() // syslog diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 0b0e0b30..f70da771 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -162,6 +162,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['level'], $handler['bubble'], $handler['file_permission'], + $handler['use_locking'], )); break; diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 12fe3f8a..3473be72 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -33,6 +33,7 @@ + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 2489bbed..36d82caa 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -323,6 +323,31 @@ public function testWithFilePermission() $this->assertSame(0777, $config['handlers']['bar']['file_permission']); } + public function testWithUseLocking() + { + $configs = array( + array( + 'handlers' => array( + 'foo' => array( + 'type' => 'stream', + 'path' => '/foo', + 'use_locking' => false, + ), + 'bar' => array( + 'type' => 'stream', + 'path' => '/bar', + 'use_locking' => true, + ) + ) + ) + ); + + $config = $this->process($configs); + + $this->assertFalse($config['handlers']['foo']['use_locking']); + $this->assertTrue($config['handlers']['bar']['use_locking']); + } + public function testWithNestedHandler() { $configs = array( diff --git a/Tests/DependencyInjection/FixtureMonologExtensionTest.php b/Tests/DependencyInjection/FixtureMonologExtensionTest.php index 8f2f9abe..b0c0f317 100644 --- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php +++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php @@ -38,7 +38,7 @@ public function testLoadWithSeveralHandlers() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, false)); $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler'); @@ -66,7 +66,7 @@ public function testLoadWithOverwriting() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true, null)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true, null, false)); $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler'); @@ -92,7 +92,7 @@ public function testLoadWithNewAtEnd() $handler = $container->getDefinition('monolog.handler.new'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true, null)); + $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true, null, false)); } public function testLoadWithNewAndPriority() @@ -124,7 +124,7 @@ public function testLoadWithNewAndPriority() $handler = $container->getDefinition('monolog.handler.last'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true, null)); + $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true, null, false)); } public function testHandlersWithChannels() diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index b70e1c50..dc1437db 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -33,14 +33,14 @@ public function testLoadWithDefault() $handler = $container->getDefinition('monolog.handler.main'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null)); + $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null, false)); $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message'))); } public function testLoadWithCustomValues() { $container = $this->getContainer(array(array('handlers' => array( - 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666') + 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666', 'use_locking' => true) )))); $this->assertTrue($container->hasDefinition('monolog.logger')); $this->assertTrue($container->hasDefinition('monolog.handler.custom')); @@ -51,7 +51,7 @@ public function testLoadWithCustomValues() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, true)); } public function testLoadWithNestedHandler() @@ -71,7 +71,7 @@ public function testLoadWithNestedHandler() $handler = $container->getDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler'); - $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666)); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, false)); } public function testLoadWithServiceHandler() From 9f6f0b6fff15c14aa3d31c9b0de83660213bb5ad Mon Sep 17 00:00:00 2001 From: dhirtzbruch Date: Sat, 5 May 2018 03:09:01 +0200 Subject: [PATCH 02/31] Allow setting "ident" parameter for SyslogUdpHandler, affects #266 --- DependencyInjection/Configuration.php | 3 ++- DependencyInjection/MonologExtension.php | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index b731acad..cc9f0016 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -138,6 +138,7 @@ * - [logopts]: defaults to LOG_PID * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true + * - [ident]: string, defaults to * * - swift_mailer: * - from_email: optional if email_prototype is given @@ -353,7 +354,7 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('filename_format')->defaultValue('{filename}-{date}')->end() //rotating ->scalarNode('date_format')->defaultValue('Y-m-d')->end() //rotating - ->scalarNode('ident')->defaultFalse()->end() // syslog + ->scalarNode('ident')->defaultFalse()->end() // syslog and syslogudp ->scalarNode('logopts')->defaultValue(LOG_PID)->end() // syslog ->scalarNode('facility')->defaultValue('user')->end() // syslog ->scalarNode('max_files')->defaultValue(0)->end() // rotating diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index c46a1a1a..95063448 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -428,6 +428,9 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['level'], $handler['bubble'], )); + if ($handler['ident']) { + $definition->addArgument($handler['ident']); + } break; case 'swift_mailer': From 86fd51e935d0a42ad34b85bdab6a3079967888bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Tue, 12 Jun 2018 12:17:55 +0200 Subject: [PATCH 03/31] Support environment option for raven client --- DependencyInjection/Configuration.php | 1 + DependencyInjection/MonologExtension.php | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 152c2ec1..704355a0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -558,6 +558,7 @@ public function getConfigTreeBuilder() ->scalarNode('client_id')->defaultNull()->end() // raven_handler ->scalarNode('auto_log_stacks')->defaultFalse()->end() // raven_handler ->scalarNode('release')->defaultNull()->end() // raven_handler + ->scalarNode('environment')->defaultNull()->end() // raven_handler ->scalarNode('message_type')->defaultValue(0)->end() // error_log ->arrayNode('tags') // loggly ->beforeNormalization() diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6b59ff55..12fdc2b8 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -605,7 +605,10 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler } else { $client = new Definition('Raven_Client', array( $handler['dsn'], - array('auto_log_stacks' => $handler['auto_log_stacks']) + array( + 'auto_log_stacks' => $handler['auto_log_stacks'], + 'environment' => $handler['environment'] + ) )); $client->setPublic(false); $clientId = 'monolog.raven.client.'.sha1($handler['dsn']); From a3790d986c32bf09b22d5f7118c628a8b39d6a15 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 15 Oct 2018 11:28:44 +0200 Subject: [PATCH 04/31] add support for redis --- DependencyInjection/Configuration.php | 58 +++++++++++++++++++ DependencyInjection/MonologExtension.php | 40 +++++++++++++ Resources/config/schema/monolog-1.0.xsd | 14 +++++ .../DependencyInjection/ConfigurationTest.php | 44 ++++++++++++++ 4 files changed, 156 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 152c2ec1..63b9fb09 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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 @@ -515,6 +527,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 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 ->arrayNode('config') ->canBeUnset() ->prototype('scalar')->end() @@ -827,6 +877,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']); }) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6b59ff55..52097c8b 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -283,6 +283,43 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['bubble'], )); 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; case 'chromephp': $definition->setArguments(array( @@ -788,6 +825,9 @@ 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])) { diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 8e7936fe..7cc54d3e 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -156,6 +156,20 @@ + + + + + + + + + + + + + + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index ffa9bf64..68700a8f 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -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. From 7fdda6731df121d579454c4355652a147653e67c Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 10 Nov 2018 20:51:15 +0100 Subject: [PATCH 05/31] Register processors for autoconfiguration --- DependencyInjection/MonologExtension.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6b59ff55..55688441 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -11,6 +11,9 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection; +use Monolog\Processor\ProcessorInterface; +use Symfony\Bridge\Monolog\Processor\TokenProcessor; +use Symfony\Bridge\Monolog\Processor\WebProcessor; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -108,6 +111,18 @@ public function load(array $configs, ContainerBuilder $container) } $container->setParameter('monolog.additional_channels', isset($config['channels']) ? $config['channels'] : array()); + + if (method_exists($container, 'registerForAutoconfiguration')) { + if (interface_exists(ProcessorInterface::class)) { + $container->registerForAutoconfiguration(ProcessorInterface::class) + ->addTag('monolog.processor'); + } else { + $container->registerForAutoconfiguration(WebProcessor::class) + ->addTag('monolog.processor'); + } + $container->registerForAutoconfiguration(TokenProcessor::class) + ->addTag('monolog.processor'); + } } /** From 24291fb4d66674a2a23afc7f00a29e6220c4039c Mon Sep 17 00:00:00 2001 From: Benoit GALATI Date: Wed, 16 Jan 2019 10:19:55 +0100 Subject: [PATCH 06/31] Remove duplicate entry --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0594999c..2b3a0265 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,6 @@ matrix: env: SYMFONY_VERSION="2.8.*" - php: 5.6 env: SYMFONY_VERSION="3.4.*" DEPENDENCIES=dev - - php: 7.1 - php: 7.0 - php: 7.1 - php: 7.2 From cc9abf64c221c9802765e6c4216832db6dc22c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Feb 2019 14:37:07 +0100 Subject: [PATCH 07/31] Expose configuration for the ConsoleHandler refs https://github.com/symfony/symfony/pull/30345 --- DependencyInjection/Configuration.php | 11 +++++++++++ DependencyInjection/MonologExtension.php | 1 + 2 files changed, 12 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 20356b50..9ca93388 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -36,6 +36,7 @@ * - [verbosity_levels]: level => verbosity configuration * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true + * - [console_formater_options]: array * * - firephp: * - [level]: level name or int value, defaults to DEBUG @@ -571,6 +572,16 @@ public function getConfigTreeBuilder() ->then(function ($v) { return array_filter(array_map('trim', $v)); }) ->end() ->prototype('scalar')->end() + ->end() + // console + ->variableNode('console_formater_options') + ->defaultValue([]) + ->validate() + ->ifTrue(function ($v) { + return !is_array($v); + }) + ->thenInvalid('console_formater_options must an array.') + ->end() ->end() ->arrayNode('verbosity_levels') // console ->beforeNormalization() diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index bd3da937..e2117f59 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -176,6 +176,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler null, $handler['bubble'], isset($handler['verbosity_levels']) ? $handler['verbosity_levels'] : array(), + $handler['console_formater_options'] )); $definition->addTag('kernel.event_subscriber'); break; From 981d74fae7e6514c5a38c985c21c178f7a687f1e Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Thu, 2 May 2019 11:14:55 +0200 Subject: [PATCH 08/31] Fix typo in PhpDoc --- DependencyInjection/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9ca93388..36fa2df8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -111,7 +111,7 @@ * - [flush_on_overflow]: bool, defaults to false * * - deduplication: - * - handler: the wrapper handler's name + * - handler: the wrapped handler's name * - [store]: The file/path where the deduplication log should be kept, defaults to %kernel.cache_dir%/monolog_dedup_* * - [deduplication_level]: The minimum logging level for log records to be looked at for deduplication purposes, defaults to ERROR * - [time]: The period (in seconds) during which duplicate entries should be suppressed after a given log is sent through, defaults to 60 From 366f6926ec3f613cb1dff4ccdeba6dae72537b4d Mon Sep 17 00:00:00 2001 From: dnna Date: Sun, 9 Sep 2018 23:10:22 +0300 Subject: [PATCH 09/31] Flush loggers on kernel.reset --- .travis.yml | 1 + CHANGELOG.md | 10 ++++++++ .../Compiler/LoggerChannelPass.php | 3 +++ DependencyInjection/Configuration.php | 12 ++++++--- DependencyInjection/MonologExtension.php | 24 ++++++++++++++++-- LICENSE | 2 +- .../Compiler/LoggerChannelPassTest.php | 11 ++++++++ .../FixtureMonologExtensionTest.php | 2 +- .../MonologExtensionTest.php | 25 +++++++++++++++++++ composer.json | 2 +- 10 files changed, 84 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7c689ac..0594999c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,7 @@ matrix: - php: 7.0 - php: 7.1 - php: 7.2 + - php: 7.3 # Test against dev versions - php: nightly env: DEPENDENCIES=dev diff --git a/CHANGELOG.md b/CHANGELOG.md index 685e6812..1d19447f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 3.3.2 (2018-12-29) + +* Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing +* Fixed regression when `app` channel is defined explicitly +* Fixed handlers marked as nested not being ignored properly from the stack + +## 3.3.1 (2018-11-04) + +* Fixed compatiblity with Symfony 4.2 + ## 3.3.0 (2018-06-04) * Fixed the autowiring of the channel logger in autoconfigured services diff --git a/DependencyInjection/Compiler/LoggerChannelPass.php b/DependencyInjection/Compiler/LoggerChannelPass.php index aa08bc1c..6fccc79b 100644 --- a/DependencyInjection/Compiler/LoggerChannelPass.php +++ b/DependencyInjection/Compiler/LoggerChannelPass.php @@ -81,6 +81,9 @@ public function process(ContainerBuilder $container) // create additional channels foreach ($container->getParameter('monolog.additional_channels') as $chan) { + if ($chan === 'app') { + continue; + } $loggerId = sprintf('monolog.logger.%s', $chan); $this->createLogger($chan, $loggerId, $container); $container->getDefinition($loggerId)->setPublic(true); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 152c2ec1..20356b50 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -293,6 +293,8 @@ * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true * + * All handlers can also be marked with `nested: true` to make sure they are never added explicitly to the stack + * * @author Jordi Boggiano * @author Christophe Coevoet */ @@ -305,8 +307,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('monolog'); + $treeBuilder = new TreeBuilder('monolog'); + $rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('monolog'); $rootNode ->fixXmlConfig('channel') @@ -342,7 +344,7 @@ public function getConfigTreeBuilder() ->booleanNode('bubble')->defaultTrue()->end() ->scalarNode('app_name')->defaultNull()->end() ->booleanNode('include_stacktraces')->defaultFalse()->end() - ->booleanNode('process_psr_3_messages')->defaultTrue()->end() + ->booleanNode('process_psr_3_messages')->defaultNull()->end() ->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end() // stream and rotating ->scalarNode('file_permission') // stream and rotating ->defaultNull() @@ -708,6 +710,10 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_http_codes']) && !empty($v['excluded_404s']); }) ->thenInvalid('You can not use excluded_http_codes together with excluded_404s in a FingersCrossedHandler') ->end() + ->validate() + ->ifTrue(function ($v) { return 'fingers_crossed' !== $v['type'] && (!empty($v['excluded_http_codes']) || !empty($v['excluded_404s'])); }) + ->thenInvalid('You can only use excluded_http_codes/excluded_404s with a FingersCrossedHandler definition') + ->end() ->validate() ->ifTrue(function ($v) { return 'filter' === $v['type'] && "DEBUG" !== $v['min_level'] && !empty($v['accepted_levels']); }) ->thenInvalid('You can not use min_level together with accepted_levels in a FilterHandler') diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6b59ff55..2ea82695 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -17,6 +17,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; +use Monolog\ResettableInterface; /** * MonologExtension is an extension for the Monolog library. @@ -131,10 +132,15 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if ('service' === $handler['type']) { $container->setAlias($handlerId, $handler['id']); + if (!empty($handler['nested']) && true === $handler['nested']) { + $this->markNestedHandler($handlerId); + } + return $handlerId; } - $definition = new Definition($this->getHandlerClassByType($handler['type'])); + $handlerClass = $this->getHandlerClassByType($handler['type']); + $definition = new Definition($handlerClass); $handler['level'] = $this->levelToMonologConst($handler['level']); @@ -142,6 +148,10 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $definition->setConfigurator(array('Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces')); } + if (null === $handler['process_psr_3_messages']) { + $handler['process_psr_3_messages'] = !isset($handler['handler']) && !$handler['members']; + } + if ($handler['process_psr_3_messages']) { $processorId = 'monolog.processor.psr_log_message'; if (!$container->hasDefinition($processorId)) { @@ -718,7 +728,12 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler break; default: - throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"', $handler['type'], $name)); + $nullWarning = ''; + if ($handler['type'] == '') { + $nullWarning = ', if you meant to define a null handler in a yaml config, make sure you quote "null" so it does not get converted to a php null'; + } + + throw new \InvalidArgumentException(sprintf('Invalid handler type "%s" given for handler "%s"' . $nullWarning, $handler['type'], $name)); } if (!empty($handler['nested']) && true === $handler['nested']) { @@ -728,6 +743,11 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (!empty($handler['formatter'])) { $definition->addMethodCall('setFormatter', array(new Reference($handler['formatter']))); } + + if (!in_array($handlerId, $this->nestedHandlers) && is_subclass_of($handlerClass, ResettableInterface::class)) { + $definition->addTag('kernel.reset', array('method' => 'reset')); + } + $container->setDefinition($handlerId, $definition); return $handlerId; diff --git a/LICENSE b/LICENSE index cdffe7ae..a677f437 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2004-2012 Fabien Potencier +Copyright (c) 2004-2019 Fabien Potencier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index 9104300b..99e9426e 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -134,6 +134,14 @@ public function testTagNotBreakingIfNoLogger() $this->assertEquals(array(), $dummyService->getArguments()); } + public function testChannelsConfigurationOptionSupportsAppChannel() + { + $container = $this->getFunctionalContainer(); + + $container->setParameter('monolog.additional_channels', array('app')); + $container->compile(); + } + private function getContainer() { $container = new ContainerBuilder(); @@ -202,6 +210,9 @@ private function getContainerWithSetter() return $container; } + /** + * @return ContainerBuilder + */ private function getFunctionalContainer() { $container = new ContainerBuilder(); diff --git a/Tests/DependencyInjection/FixtureMonologExtensionTest.php b/Tests/DependencyInjection/FixtureMonologExtensionTest.php index f7b1b51c..d5f9aa22 100644 --- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php +++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php @@ -202,7 +202,7 @@ public function testPsr3MessageProcessingEnabled() $methodCalls = $logger->getMethodCalls(); - $this->assertContains(array('pushProcessor', array(new Reference('monolog.processor.psr_log_message'))), $methodCalls, 'The PSR-3 processor should not be enabled', false, false); + $this->assertContains(array('pushProcessor', array(new Reference('monolog.processor.psr_log_message'))), $methodCalls, 'The PSR-3 processor should be enabled', false, false); } public function testPsr3MessageProcessingDisabled() diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index 0f8ec45a..0310fe16 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -84,6 +84,31 @@ public function testLoadWithServiceHandler() $this->assertTrue($container->hasDefinition('monolog.logger')); $this->assertTrue($container->hasAlias('monolog.handler.custom')); + $logger = $container->getDefinition('monolog.logger'); + // Custom service handler must be pushed to logger + $this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%')); + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); + + $handler = $container->findDefinition('monolog.handler.custom'); + $this->assertDICDefinitionClass($handler, 'stdClass'); + $this->assertDICConstructorArguments($handler, array('foo', false)); + } + + public function testLoadWithNestedServiceHandler() + { + $container = $this->getContainer( + array(array('handlers' => array('custom' => array('type' => 'service', 'id' => 'some.service.id', 'nested' => true)))), + array('some.service.id' => new Definition('stdClass', array('foo', false))) + ); + + $this->assertTrue($container->hasDefinition('monolog.logger')); + $this->assertTrue($container->hasAlias('monolog.handler.custom')); + + $logger = $container->getDefinition('monolog.logger'); + // Nested service handler must not be pushed to logger + $this->assertCount(1, $logger->getMethodCalls()); + $this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%')); + $handler = $container->findDefinition('monolog.handler.custom'); $this->assertDICDefinitionClass($handler, 'stdClass'); $this->assertDICConstructorArguments($handler, array('foo', false)); diff --git a/composer.json b/composer.json index c2d58944..8b091ca8 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require-dev": { "symfony/yaml": "~2.7|~3.3|~4.0", "symfony/console": "~2.7|~3.3|~4.0", - "symfony/phpunit-bridge": "^3.3|^4.0" + "symfony/phpunit-bridge": "^3.4.19|^4.0" }, "autoload": { "psr-4": { "Symfony\\Bundle\\MonologBundle\\": "" }, From a7c10464fe65642514e7ef8e2556f67589fe4001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 16 May 2019 12:24:37 +0200 Subject: [PATCH 10/31] Fixed CS and CHANGELOG --- CHANGELOG.md | 5 ++++- DependencyInjection/MonologExtension.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d19447f..0368bda9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ -## 3.3.2 (2018-12-29) +## 3.4.0 (xxxx-xx-xx) +* Flush loggers on `kernel.reset` +* Register processors (`ProcessorInterface`) for autoconfiguration (tag: `monolog.processor`) +* Expose configuration for the `ConsoleHandler` * Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing * Fixed regression when `app` channel is defined explicitly * Fixed handlers marked as nested not being ignored properly from the stack diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index fe62839b..613e883b 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\MonologBundle\DependencyInjection; use Monolog\Processor\ProcessorInterface; +use Monolog\ResettableInterface; use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -20,7 +21,6 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; -use Monolog\ResettableInterface; /** * MonologExtension is an extension for the Monolog library. From f6c859332024b49831363f837a3ff8f606841bfd Mon Sep 17 00:00:00 2001 From: Geoffrey Brier Date: Thu, 26 Jul 2018 14:30:13 +0200 Subject: [PATCH 11/31] Deprecrate "excluded_404s" option --- CHANGELOG.md | 2 ++ DependencyInjection/MonologExtension.php | 4 ++++ Tests/DependencyInjection/MonologExtensionTest.php | 1 + 3 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0368bda9..7a70bbd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 3.4.0 (xxxx-xx-xx) +* Deprecate "excluded_404s" option * Flush loggers on `kernel.reset` * Register processors (`ProcessorInterface`) for autoconfiguration (tag: `monolog.processor`) * Expose configuration for the `ConsoleHandler` @@ -17,6 +18,7 @@ * Added timeouts to the pushover, hipchat, slack handlers * Dropped support for PHP 5.3, 5.4, and HHVM * Added configuration for HttpCodeActivationStrategy +* Deprecated "excluded_404s" option for Symfony >= 3.4 ## 3.2.0 (2018-03-05) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 613e883b..69bafece 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -13,6 +13,7 @@ use Monolog\Processor\ProcessorInterface; use Monolog\ResettableInterface; +use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy; use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -343,6 +344,9 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (isset($handler['activation_strategy'])) { $activation = new Reference($handler['activation_strategy']); } elseif (!empty($handler['excluded_404s'])) { + if (class_exists(HttpCodeActivationStrategy::class)) { + @trigger_error('The "excluded_404s" option is deprecated in MonologBundle since version 3.4.0, you should rely on the "excluded_http_codes" option instead.', E_USER_DEPRECATED); + } $activationDef = new Definition('Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy', array( new Reference('request_stack'), $handler['excluded_404s'], diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index 0310fe16..366eaaf7 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -383,6 +383,7 @@ public function testLogglyHandler() $this->assertDICDefinitionMethodCallAt(1, $handler, 'setTag', array('foo,bar')); } + /** @group legacy */ public function testFingersCrossedHandlerWhenExcluded404sAreSpecified() { $container = $this->getContainer(array(array('handlers' => array( From 9f8e531bd81fea89be2f94e833aa242ce18702e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 16 May 2019 12:50:27 +0200 Subject: [PATCH 12/31] Fixed PHPUnit Warning --- .travis.yml | 2 ++ Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php | 3 +++ 2 files changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2b3a0265..00141522 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,8 @@ matrix: env: SYMFONY_VERSION="3.4.*" DEPENDENCIES=dev - php: 7.0 - php: 7.1 + # There is a bug in PHPUnit 5.7 + env: SYMFONY_PHPUNIT_VERSION=6.5 - php: 7.2 - php: 7.3 # Test against dev versions diff --git a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php index 99e9426e..7743cbf7 100644 --- a/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerChannelPassTest.php @@ -140,6 +140,9 @@ public function testChannelsConfigurationOptionSupportsAppChannel() $container->setParameter('monolog.additional_channels', array('app')); $container->compile(); + + // the test ensures that the validation does not fail (i.e. it does not throw any exceptions) + $this->addToAssertionCount(1); } private function getContainer() From 2e0714e0c2932e67ec45c07375cbe6605253c496 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:48:00 +0200 Subject: [PATCH 13/31] add better explanation of wat an id is --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cdef5669..8c6969b9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -547,7 +547,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Redis client.') ->end() ->end() // redis ->arrayNode('predis') @@ -564,7 +564,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Predis client.') ->end() ->end() // predis ->arrayNode('config') From 100cfded402baafc4e4f35cef4180e43d2f81951 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:52:06 +0200 Subject: [PATCH 14/31] fix handler key not correct --- DependencyInjection/MonologExtension.php | 49 ++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 0dbead71..3dacc4f8 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -293,37 +293,39 @@ 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 { + $type = $handler['type']; + + if (isset($handler[$type]['id'])) { + $clientId = $handler[$type]['id']; + } elseif ('redis' === $type) { + if (!class_exists('\Redis')) { throw new \RuntimeException('The \Redis is not available.'); } + + $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')) { - $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 { + if (!class_exists('\Predis\Client')) { throw new \RuntimeException('The \Predis\Client is not available.'); } + + $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['redis']['key_name'], + $handler[$type]['key_name'], $handler['level'], $handler['bubble'], )); @@ -840,7 +842,6 @@ private function getHandlerClassByType($handlerType) 'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler', 'redis' => 'Monolog\Handler\RedisHandler', 'predis' => 'Monolog\Handler\RedisHandler', - ); if (!isset($typeToClassMapping[$handlerType])) { From a14341679498069d13d09fb5e9e52558627d7537 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 15 Oct 2018 11:28:44 +0200 Subject: [PATCH 15/31] add support for redis --- DependencyInjection/Configuration.php | 58 +++++++++++++++++++ DependencyInjection/MonologExtension.php | 40 +++++++++++++ Resources/config/schema/monolog-1.0.xsd | 14 +++++ .../DependencyInjection/ConfigurationTest.php | 44 ++++++++++++++ 4 files changed, 156 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ff023175..eb7dbafb 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -88,6 +88,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 @@ -522,6 +534,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 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 ->arrayNode('config') ->canBeUnset() ->prototype('scalar')->end() @@ -849,6 +899,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']); }) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 14b00662..6f351fec 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -311,6 +311,43 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['bubble'], )); 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; case 'chromephp': $definition->setArguments(array( @@ -835,6 +872,9 @@ 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])) { diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 137df65e..cf0eac87 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -157,6 +157,20 @@ + + + + + + + + + + + + + + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index a59c75d6..aa7a5b3c 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -362,6 +362,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. From f07d51bdfd54999b002124bc5efd9fbbea606e4e Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:48:00 +0200 Subject: [PATCH 16/31] add better explanation of wat an id is --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index eb7dbafb..f6a7204d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -552,7 +552,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Redis client.') ->end() ->end() // redis ->arrayNode('predis') @@ -569,7 +569,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Predis client.') ->end() ->end() // predis ->arrayNode('config') From 9c992e14a8943920e16c27ee99ab50b78d8bda06 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:52:06 +0200 Subject: [PATCH 17/31] fix handler key not correct --- DependencyInjection/MonologExtension.php | 49 ++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6f351fec..e4111c9a 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -313,37 +313,39 @@ 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 { + $type = $handler['type']; + + if (isset($handler[$type]['id'])) { + $clientId = $handler[$type]['id']; + } elseif ('redis' === $type) { + if (!class_exists('\Redis')) { throw new \RuntimeException('The \Redis is not available.'); } + + $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')) { - $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 { + if (!class_exists('\Predis\Client')) { throw new \RuntimeException('The \Predis\Client is not available.'); } + + $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['redis']['key_name'], + $handler[$type]['key_name'], $handler['level'], $handler['bubble'], )); @@ -874,7 +876,6 @@ private function getHandlerClassByType($handlerType) 'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler', 'redis' => 'Monolog\Handler\RedisHandler', 'predis' => 'Monolog\Handler\RedisHandler', - ); if (!isset($typeToClassMapping[$handlerType])) { From b43daff66339754b8116e1e474fe5313dddf8a09 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:28:17 +0200 Subject: [PATCH 18/31] fix exception messages when a certain redis class does not exist --- DependencyInjection/MonologExtension.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index e4111c9a..532c0bef 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -13,6 +13,8 @@ use Monolog\Processor\ProcessorInterface; use Monolog\ResettableInterface; +use Predis; +use Redis; use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy; use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; @@ -318,8 +320,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (isset($handler[$type]['id'])) { $clientId = $handler[$type]['id']; } elseif ('redis' === $type) { - if (!class_exists('\Redis')) { - throw new \RuntimeException('The \Redis is not available.'); + if (!class_exists(Redis::class)) { + throw new \RuntimeException('The \Redis class is not available.'); } $client = new Definition('\Redis'); @@ -330,8 +332,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $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.'); + if (!class_exists(Predis\Client::class)) { + throw new \RuntimeException('The \Predis\Client class is not available.'); } $client = new Definition('\Predis\Client'); From 7a1dcab7f6bfbf75d8ddfddf3a5d7f01308a099c Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:30:21 +0200 Subject: [PATCH 19/31] add changelog rule --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a70bbd3..f73ca5d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing * Fixed regression when `app` channel is defined explicitly * Fixed handlers marked as nested not being ignored properly from the stack +* Added configuration support for Redis ## 3.3.1 (2018-11-04) From 56709bcdd3ba02a08340ab530da974debc4967e6 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:56:38 +0200 Subject: [PATCH 20/31] add enter between types --- DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f6a7204d..64134457 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -100,6 +100,7 @@ * - 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 From e2fc27d028d9797f4504df1361bd2ed22c5d771a Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 13:26:07 +0200 Subject: [PATCH 21/31] change back type and fix configuration --- DependencyInjection/Configuration.php | 20 +++++++++++--------- DependencyInjection/MonologExtension.php | 12 +++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 64134457..4eea7fe1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -89,17 +89,19 @@ * - [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 + * - 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 + * - redis: + * - 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 diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 532c0bef..02bfbecd 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -315,11 +315,9 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler break; case 'redis': case 'predis': - $type = $handler['type']; - - if (isset($handler[$type]['id'])) { - $clientId = $handler[$type]['id']; - } elseif ('redis' === $type) { + if (isset($handler['redis']['id'])) { + $clientId = $handler['redis']['id']; + } elseif ('redis' === $handler['type']) { if (!class_exists(Redis::class)) { throw new \RuntimeException('The \Redis class is not available.'); } @@ -338,7 +336,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $client = new Definition('\Predis\Client'); $client->setArguments(array( - $handler['predis']['host'], + $handler['redis']['host'], )); $client->setPublic(false); @@ -347,7 +345,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler } $definition->setArguments(array( new Reference($clientId), - $handler[$type]['key_name'], + $handler['redis']['key_name'], $handler['level'], $handler['bubble'], )); From 5448621719584897af28cb9fde69e5d76c546c05 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 15 Oct 2018 11:28:44 +0200 Subject: [PATCH 22/31] add support for redis --- DependencyInjection/Configuration.php | 58 +++++++++++++++++++ DependencyInjection/MonologExtension.php | 40 +++++++++++++ Resources/config/schema/monolog-1.0.xsd | 14 +++++ .../DependencyInjection/ConfigurationTest.php | 44 ++++++++++++++ 4 files changed, 156 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ff023175..eb7dbafb 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -88,6 +88,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 @@ -522,6 +534,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 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 ->arrayNode('config') ->canBeUnset() ->prototype('scalar')->end() @@ -849,6 +899,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']); }) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 14b00662..6f351fec 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -311,6 +311,43 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['bubble'], )); 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; case 'chromephp': $definition->setArguments(array( @@ -835,6 +872,9 @@ 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])) { diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 137df65e..cf0eac87 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -157,6 +157,20 @@ + + + + + + + + + + + + + + diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index a59c75d6..aa7a5b3c 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -362,6 +362,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. From 4a01df888ee59e9ca7255b8af21b24f01d190534 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:48:00 +0200 Subject: [PATCH 23/31] add better explanation of wat an id is --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index eb7dbafb..f6a7204d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -552,7 +552,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Redis client.') ->end() ->end() // redis ->arrayNode('predis') @@ -569,7 +569,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) - ->thenInvalid('What must be set is either the host or the id.') + ->thenInvalid('What must be set is either the host or the service id of the Predis client.') ->end() ->end() // predis ->arrayNode('config') From eabdb9cda7033bc0029734750a8d3504424baafd Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:52:06 +0200 Subject: [PATCH 24/31] fix handler key not correct --- DependencyInjection/MonologExtension.php | 49 ++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 6f351fec..e4111c9a 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -313,37 +313,39 @@ 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 { + $type = $handler['type']; + + if (isset($handler[$type]['id'])) { + $clientId = $handler[$type]['id']; + } elseif ('redis' === $type) { + if (!class_exists('\Redis')) { throw new \RuntimeException('The \Redis is not available.'); } + + $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')) { - $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 { + if (!class_exists('\Predis\Client')) { throw new \RuntimeException('The \Predis\Client is not available.'); } + + $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['redis']['key_name'], + $handler[$type]['key_name'], $handler['level'], $handler['bubble'], )); @@ -874,7 +876,6 @@ private function getHandlerClassByType($handlerType) 'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler', 'redis' => 'Monolog\Handler\RedisHandler', 'predis' => 'Monolog\Handler\RedisHandler', - ); if (!isset($typeToClassMapping[$handlerType])) { From ad6b37fe815c1e32157fb4bba9ebe55fe4edfb7a Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 15 Oct 2018 11:28:44 +0200 Subject: [PATCH 25/31] add support for redis --- DependencyInjection/Configuration.php | 8 ++++ DependencyInjection/MonologExtension.php | 48 ++++++++++++------------ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f6a7204d..05250a9f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -552,7 +552,11 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) +<<<<<<< HEAD ->thenInvalid('What must be set is either the host or the service id of the Redis client.') +======= + ->thenInvalid('What must be set is either the host or the id.') +>>>>>>> add support for redis ->end() ->end() // redis ->arrayNode('predis') @@ -569,7 +573,11 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) +<<<<<<< HEAD ->thenInvalid('What must be set is either the host or the service id of the Predis client.') +======= + ->thenInvalid('What must be set is either the host or the id.') +>>>>>>> add support for redis ->end() ->end() // predis ->arrayNode('config') diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index e4111c9a..49fb3fe4 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -313,39 +313,37 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler break; case 'redis': case 'predis': - $type = $handler['type']; - - if (isset($handler[$type]['id'])) { - $clientId = $handler[$type]['id']; - } elseif ('redis' === $type) { - if (!class_exists('\Redis')) { + 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.'); } - - $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')) { + 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.'); } - - $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['redis']['key_name'], $handler['level'], $handler['bubble'], )); From e23d1ff74029fb5d465d3df33b07616af43c33d0 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:48:00 +0200 Subject: [PATCH 26/31] add better explanation of wat an id is --- DependencyInjection/Configuration.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 05250a9f..f6a7204d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -552,11 +552,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) -<<<<<<< HEAD ->thenInvalid('What must be set is either the host or the service id of the Redis client.') -======= - ->thenInvalid('What must be set is either the host or the id.') ->>>>>>> add support for redis ->end() ->end() // redis ->arrayNode('predis') @@ -573,11 +569,7 @@ public function getConfigTreeBuilder() ->ifTrue(function ($v) { return !isset($v['id']) && !isset($v['host']); }) -<<<<<<< HEAD ->thenInvalid('What must be set is either the host or the service id of the Predis client.') -======= - ->thenInvalid('What must be set is either the host or the id.') ->>>>>>> add support for redis ->end() ->end() // predis ->arrayNode('config') From d1ca48b11c848a01c8b169395205b7966762b20b Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Tue, 28 May 2019 08:52:06 +0200 Subject: [PATCH 27/31] fix handler key not correct --- DependencyInjection/MonologExtension.php | 48 ++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 49fb3fe4..e4111c9a 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -313,37 +313,39 @@ 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 { + $type = $handler['type']; + + if (isset($handler[$type]['id'])) { + $clientId = $handler[$type]['id']; + } elseif ('redis' === $type) { + if (!class_exists('\Redis')) { throw new \RuntimeException('The \Redis is not available.'); } + + $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')) { - $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 { + if (!class_exists('\Predis\Client')) { throw new \RuntimeException('The \Predis\Client is not available.'); } + + $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['redis']['key_name'], + $handler[$type]['key_name'], $handler['level'], $handler['bubble'], )); From 2e09bfd3a55f7c2776eadb549e2a588f3c297c70 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:28:17 +0200 Subject: [PATCH 28/31] fix exception messages when a certain redis class does not exist --- DependencyInjection/MonologExtension.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index e4111c9a..532c0bef 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -13,6 +13,8 @@ use Monolog\Processor\ProcessorInterface; use Monolog\ResettableInterface; +use Predis; +use Redis; use Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy; use Symfony\Bridge\Monolog\Processor\TokenProcessor; use Symfony\Bridge\Monolog\Processor\WebProcessor; @@ -318,8 +320,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (isset($handler[$type]['id'])) { $clientId = $handler[$type]['id']; } elseif ('redis' === $type) { - if (!class_exists('\Redis')) { - throw new \RuntimeException('The \Redis is not available.'); + if (!class_exists(Redis::class)) { + throw new \RuntimeException('The \Redis class is not available.'); } $client = new Definition('\Redis'); @@ -330,8 +332,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $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.'); + if (!class_exists(Predis\Client::class)) { + throw new \RuntimeException('The \Predis\Client class is not available.'); } $client = new Definition('\Predis\Client'); From 5d8ee96278f87bfa9c64edd3270698cc61bda6e6 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:30:21 +0200 Subject: [PATCH 29/31] add changelog rule --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a70bbd3..f73ca5d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fixed psr-3 processing being applied to all handlers, only leaf ones are now processing * Fixed regression when `app` channel is defined explicitly * Fixed handlers marked as nested not being ignored properly from the stack +* Added configuration support for Redis ## 3.3.1 (2018-11-04) From ec0dc0ce02eb0d4237260d75298aa295509be3ec Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 11:56:38 +0200 Subject: [PATCH 30/31] add enter between types --- DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f6a7204d..64134457 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -100,6 +100,7 @@ * - 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 From 7a012aff8c9d009c763d5897d4c115c99c3e54ca Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Mon, 3 Jun 2019 13:26:07 +0200 Subject: [PATCH 31/31] change back type and fix configuration --- DependencyInjection/Configuration.php | 20 +++++++++++--------- DependencyInjection/MonologExtension.php | 12 +++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 64134457..4eea7fe1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -89,17 +89,19 @@ * - [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 + * - 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 + * - redis: + * - 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 diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 532c0bef..02bfbecd 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -315,11 +315,9 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler break; case 'redis': case 'predis': - $type = $handler['type']; - - if (isset($handler[$type]['id'])) { - $clientId = $handler[$type]['id']; - } elseif ('redis' === $type) { + if (isset($handler['redis']['id'])) { + $clientId = $handler['redis']['id']; + } elseif ('redis' === $handler['type']) { if (!class_exists(Redis::class)) { throw new \RuntimeException('The \Redis class is not available.'); } @@ -338,7 +336,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $client = new Definition('\Predis\Client'); $client->setArguments(array( - $handler['predis']['host'], + $handler['redis']['host'], )); $client->setPublic(false); @@ -347,7 +345,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler } $definition->setArguments(array( new Reference($clientId), - $handler[$type]['key_name'], + $handler['redis']['key_name'], $handler['level'], $handler['bubble'], ));