8000 feature #279 Flush loggers on kernel.reset (dnna) · symfony/monolog-bundle@6684794 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6684794

Browse files
committed
feature #279 Flush loggers on kernel.reset (dnna)
This PR was squashed before being merged into the 3.x-dev branch (closes #279). Discussion ---------- Flush loggers on kernel.reset Fixes php-pm/php-pm-httpkernel#134 and php-pm/php-pm-httpkernel#62 When using [PHP-PM](https://github.com/php-pm/php-pm) the worker does not die at the end of the request, so the shutdown functions registered by monolog are never triggered (so for example `BufferHandler` never flushes). This PR partially fixes the problem by flushing the loggers containing a `flush` function at the end of each request. I did not use the `close` function (mentioned in Seldaek/monolog#1053) because some loggers use sockets or other connections that would be closed in this case and not be reusable in the next request. I'm not sure if this is the best solution to this problem, happy to discuss alternatives if there is a better way. Commits ------- 366f692 Flush loggers on kernel.reset
2 parents 962a2e1 + 366f692 commit 6684794

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Config\FileLocator;
2121
use Symfony\Component\DependencyInjection\Definition;
2222
use Symfony\Component\DependencyInjection\Reference;
23+
use Monolog\ResettableInterface;
2324

2425
/**
2526
* MonologExtension is an extension for the Monolog library.
@@ -153,7 +154,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
153154
return $handlerId;
154155
}
155156

156-
$definition = new Definition($this->getHandlerClassByType($handler['type']));
157+
$handlerClass = $this->getHandlerClassByType($handler['type']);
158+
$definition = new Definition($handlerClass);
157159

158160
$handler['level'] = $this->levelToMonologConst($handler['level']);
159161

@@ -757,6 +759,11 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
757759
if (!empty($handler['formatter'])) {
758760
$definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
759761
}
762+
763+
if (!in_array($handlerId, $this->nestedHandlers) && is_subclass_of($handlerClass, ResettableInterface::class)) {
764+
$definition->addTag('kernel.reset', array('method' => 'reset'));
765+
}
766+
760767
$container->setDefinition($handlerId, $definition);
761768

762769
return $handlerId;

0 commit comments

Comments
 (0)
0