From 5c101616a182d15a046c99194ce51d55693f465a Mon Sep 17 00:00:00 2001 From: Andy Clyde Date: Tue, 24 Nov 2020 10:35:59 +0000 Subject: [PATCH 1/3] Ensure all handlers are explicitly closed on kernel shutdown Fixes #376 --- DependencyInjection/MonologExtension.php | 2 ++ MonologBundle.php | 11 ++++++++ Resources/config/monolog.xml | 4 +++ Services/HandlerManager.php | 28 +++++++++++++++++++ .../MonologExtensionTest.php | 10 +++++++ 5 files changed, 55 insertions(+) create mode 100644 Services/HandlerManager.php diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index e1ac10a8..5cbbf72a 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -933,6 +933,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $definition->addTag('kernel.reset', ['method' => 'reset']); } + $definition->addTag('monolog.handler'); + $container->setDefinition($handlerId, $definition); return $handlerId; diff --git a/MonologBundle.php b/MonologBundle.php index ba159196..739f1ac5 100644 --- a/MonologBundle.php +++ b/MonologBundle.php @@ -52,4 +52,15 @@ public static function includeStacktraces(HandlerInterface $handler) $formatter->includeStacktraces(); } } + + public function shutdown() + { + parent::shutdown(); + + $handlerManager = $this->container->get('monolog.handler_manager'); + + $handlerManager->close(); + } + + } diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index 3f62ed57..caeeb697 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -40,5 +40,9 @@ + + + + diff --git a/Services/HandlerManager.php b/Services/HandlerManager.php new file mode 100644 index 00000000..41bc0849 --- /dev/null +++ b/Services/HandlerManager.php @@ -0,0 +1,28 @@ +handlers = $handlers; + } + + public function close() + { + foreach ($this->handlers as $handler) { + $handler->close(); + } + } +} diff --git a/Tests/DependencyInjection/MonologExtensionTest.php b/Tests/DependencyInjection/MonologExtensionTest.php index fe408186..18d6f569 100644 --- a/Tests/DependencyInjection/MonologExtensionTest.php +++ b/Tests/DependencyInjection/MonologExtensionTest.php @@ -89,6 +89,16 @@ public function testLoadWithNestedHandler() $this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, false]); } + public function testTagsAllHandlersCreated() + { + $container = $this->getContainer([['handlers' => [ + 'custom' => ['type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666'], + 'nested' => ['type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666', 'nested' => true] + ]]]); + $taggedHandlers = $container->findTaggedServiceIds('monolog.handler'); + $this->assertCount(2, $taggedHandlers); + } + public function testLoadWithServiceHandler() { $container = $this->getContainer( From b166b7c6529493cc6da32dee678bcb24af2800e8 Mon Sep 17 00:00:00 2001 From: Andy Clyde Date: Tue, 24 Nov 2020 13:46:08 +0000 Subject: [PATCH 2/3] Fixes after PR comments --- Services/HandlerManager.php => HandlerLifecycleManager.php | 7 +++---- MonologBundle.php | 1 - Resources/config/monolog.xml | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) rename Services/HandlerManager.php => HandlerLifecycleManager.php (71%) diff --git a/Services/HandlerManager.php b/HandlerLifecycleManager.php similarity index 71% rename from Services/HandlerManager.php rename to HandlerLifecycleManager.php index 41bc0849..3f6381ff 100644 --- a/Services/HandlerManager.php +++ b/HandlerLifecycleManager.php @@ -1,20 +1,19 @@ handlers = $handlers; } diff --git a/MonologBundle.php b/MonologBundle.php index 739f1ac5..19f976ff 100644 --- a/MonologBundle.php +++ b/MonologBundle.php @@ -62,5 +62,4 @@ public function shutdown() $handlerManager->close(); } - } diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index caeeb697..325d570c 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -41,8 +41,8 @@ - - + + From 954a545d740421970d28763d6a0feafd70287c09 Mon Sep 17 00:00:00 2001 From: Andy Clyde Date: Fri, 27 Nov 2020 11:50:07 +0000 Subject: [PATCH 3/3] Tweaks after testing against local Symfony 5 project --- HandlerLifecycleManager.php | 2 +- Resources/config/monolog.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HandlerLifecycleManager.php b/HandlerLifecycleManager.php index 3f6381ff..0ddcc956 100644 --- a/HandlerLifecycleManager.php +++ b/HandlerLifecycleManager.php @@ -13,7 +13,7 @@ class HandlerLifecycleManager */ private $handlers; - public function __construct(iterable $handlers) + public function __construct(\IteratorAggregate $handlers) { $this->handlers = $handlers; } diff --git a/Resources/config/monolog.xml b/Resources/config/monolog.xml index 325d570c..90c6dcee 100644 --- a/Resources/config/monolog.xml +++ b/Resources/config/monolog.xml @@ -41,7 +41,7 @@ - +