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 all commits
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
27 changes: 27 additions & 0 deletions HandlerLifecycleManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Symfony\Bundle\MonologBundle;


use Monolog\Handler\HandlerInterface;

class HandlerLifecycleManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be tagged as @internal. We really don't want to expose it in the BC promise.

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

public function __construct(\IteratorAggregate $handlers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct(\IteratorAggregate $handlers)
public function __construct(iterable $handlers)

The type IteratorAggregate is a bit more specific that it needs to be, isn‘t it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bundle supports PHP 5.6+ currently

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but Traversable should be used instead of IteratorAggregate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should drop support for SF 3.X, and bump PHP to 7.1
IMHO, we must do that before this PR

{
$this->handlers = $handlers;
}

public function close()
{
foreach ($this->handlers as $handler) {
$handler->close();
}
}
}
10 changes: 10 additions & 0 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ 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\HandlerLifecycleManager" public="true">
<argument type="tagged_iterator" tag="monolog.handler" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using type="tagged_iterator", you should use a compiler pass to create the IteratorArgument (or drop the tag entirely and manage everything in the DI extension as I don't think we need to support handlers added elsewhere). This way, you can use weak references in the IteratorArgument, which will skip services which haven't been instantiated yet

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm familiar enough with the Container to change this right now but given #377 (comment) I should probably pause on this PR now anyway?

</service>
</services>
</container>
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