Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all handlers are explicitly closed on kernel shutdown #377

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}


lyrixx marked this conversation as resolved.
Show resolved Hide resolved
}
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" />
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
</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;
lyrixx marked this conversation as resolved.
Show resolved Hide resolved


use Monolog\Handler\HandlerInterface;

class HandlerManager
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
{

lyrixx marked this conversation as resolved.
Show resolved Hide resolved
/**
* @var HandlerInterface[]
*/
private $handlers;

public function __construct(array $handlers)
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
{
$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