From f052b313890d653f746f02916b0c073a87475e8a Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 4 Nov 2025 12:24:57 +0100 Subject: [PATCH 1/2] Added the handler_class option to customize a handler while keeping its original type. --- config/schema/monolog-1.0.xsd | 1 + src/DependencyInjection/Configuration.php | 1 + src/DependencyInjection/MonologExtension.php | 10 +++++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config/schema/monolog-1.0.xsd b/config/schema/monolog-1.0.xsd index bc9c6e55..8ffa40b6 100644 --- a/config/schema/monolog-1.0.xsd +++ b/config/schema/monolog-1.0.xsd @@ -95,6 +95,7 @@ + diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 36e8d007..784b1c53 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -661,6 +661,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->scalarNode('formatter')->end() ->booleanNode('nested')->defaultFalse()->end() + ->scalarNode('handler_class')->defaultNull()->end() ->end(); $this->addGelfSection($handlerNode); diff --git a/src/DependencyInjection/MonologExtension.php b/src/DependencyInjection/MonologExtension.php index 235f3804..7ee9dd65 100644 --- a/src/DependencyInjection/MonologExtension.php +++ b/src/DependencyInjection/MonologExtension.php @@ -166,7 +166,15 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler return $handlerId; } - $handlerClass = $this->getHandlerClassByType($handler['type']); + if (null !== $handler['handler_class']) { + $handlerClass = $handler['handler_class']; + if (!class_exists($handlerClass)) { + throw new \RuntimeException(sprintf('The handler class "%s" does not exist.', $handlerClass)); + } + } else { + $handlerClass = $this->getHandlerClassByType($handler['type']); + } + $definition = new Definition($handlerClass); if ($handler['include_stacktraces']) { From fb4a858254c176bd2deb12da1242dd00d109b170 Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 4 Nov 2025 12:39:04 +0100 Subject: [PATCH 2/2] fix php-cs-fixer --- src/DependencyInjection/MonologExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DependencyInjection/MonologExtension.php b/src/DependencyInjection/MonologExtension.php index 7ee9dd65..c41e029a 100644 --- a/src/DependencyInjection/MonologExtension.php +++ b/src/DependencyInjection/MonologExtension.php @@ -169,7 +169,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler if (null !== $handler['handler_class']) { $handlerClass = $handler['handler_class']; if (!class_exists($handlerClass)) { - throw new \RuntimeException(sprintf('The handler class "%s" does not exist.', $handlerClass)); + throw new \RuntimeException(\sprintf('The handler class "%s" does not exist.', $handlerClass)); } } else { $handlerClass = $this->getHandlerClassByType($handler['type']);