Skip to content

Commit 5c10161

Browse files
committed
Ensure all handlers are explicitly closed on kernel shutdown
Fixes #376
1 parent a5b28e1 commit 5c10161

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
933933
$definition->addTag('kernel.reset', ['method' => 'reset']);
934934
}
935935

936+
$definition->addTag('monolog.handler');
937+
936938
$container->setDefinition($handlerId, $definition);
937939

938940
return $handlerId;

MonologBundle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ public static function includeStacktraces(HandlerInterface $handler)
5252
$formatter->includeStacktraces();
5353
}
5454
}
55+
56+
public function shutdown()
57+
{
58+
parent::shutdown();
59+
60+
$handlerManager = $this->container->get('monolog.handler_manager');
61+
62+
$handlerManager->close();
63+
}
64+
65+
5566
}

Resources/config/monolog.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@
4040
<service id="monolog.http_client" class="Symfony\Contracts\HttpClient\HttpClientInterface" public="false">
4141
<factory class="Symfony\Component\HttpClient\HttpClient" method="create" />
4242
</service>
43+
44+
<service id="monolog.handler_manager" class="Symfony\Bundle\MonologBundle\Services\HandlerManager" public="false">
45+
<argument type="tagged" tag="monolog.handler" />
46+
</service>
4347
</services>
4448
</container>

Services/HandlerManager.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
4+
namespace Symfony\Bundle\MonologBundle\Services;
5+
6+
7+
use Monolog\Handler\HandlerInterface;
8+
9+
class HandlerManager
10+
{
11+
12+
/**
13+
* @var HandlerInterface[]
14+
*/
15+
private $handlers;
16+
17+
public function __construct(array $handlers)
18+
{
19+
$this->handlers = $handlers;
20+
}
21+
22+
public function close()
23+
{
24+
foreach ($this->handlers as $handler) {
25+
$handler->close();
26+
}
27+
}
28+
}

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public function testLoadWithNestedHandler()
8989
$this->assertDICConstructorArguments($handler, ['/tmp/symfony.log', \Monolog\Logger::ERROR, false, 0666, false]);
9090
}
9191

92+
public function testTagsAllHandlersCreated()
93+
{
94+
$container = $this->getContainer([['handlers' => [
95+
'custom' => ['type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666'],
96+
'nested' => ['type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR', 'file_permission' => '0666', 'nested' => true]
97+
]]]);
98+
$taggedHandlers = $container->findTaggedServiceIds('monolog.handler');
99+
$this->assertCount(2, $taggedHandlers);
100+
}
101+
92102
public function testLoadWithServiceHandler()
93103
{
94104
$container = $this->getContainer(

0 commit comments

Comments
 (0)