Skip to content

Commit

Permalink
Ensure all handlers are explicitly closed on kernel shutdown
Browse files Browse the repository at this point in the history
Fixes #376
  • Loading branch information
andy-educake committed Nov 24, 2020
1 parent a5b28e1 commit 5c10161
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


}
4 changes: 4 additions & 0 deletions Resources/config/monolog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
<service id="monolog.http_client" class="Symfony\Contracts\HttpClient\HttpClientInterface" public="false">
<factory class="Symfony\Component\HttpClient\HttpClient" method="create" />
</service>

<service id="monolog.handler_manager" class="Symfony\Bundle\MonologBundle\Services\HandlerManager" public="false">
<argument type="tagged" tag="monolog.handler" />
</service>
</services>
</container>
28 changes: 28 additions & 0 deletions Services/HandlerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace Symfony\Bundle\MonologBundle\Services;


use Monolog\Handler\HandlerInterface;

class HandlerManager
{

/**
* @var HandlerInterface[]
*/
private $handlers;

public function __construct(array $handlers)
{
$this->handlers = $handlers;
}

public function close()
{
foreach ($this->handlers as $handler) {
$handler->close();
}
}
}
10 changes: 10 additions & 0 deletions Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5c10161

Please sign in to comment.