Skip to content

[Monolog] Use a standalone http client for ElasticsearchLogstashHandler #34423

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Bridge\Twig\Extension\CsrfExtension;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -43,6 +44,7 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
Expand Down Expand Up @@ -446,6 +448,11 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
->addMethodCall('setLogger', [new Reference('logger')]);

$handlerAutoconfiguration = $container->registerForAutoconfiguration(ElasticsearchLogstashHandler::class);
Copy link
Member

Choose a reason for hiding this comment

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

I would bind on HandlerInterface instead - all handlers that need an http_client cannot use the default one since it logs.

$handlerAutoconfiguration->setBindings($handlerAutoconfiguration->getBindings() + [
HttpClientInterface::class => new BoundArgument(new Reference('monolog.http_client'), false),
]);

if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers
$container->getDefinition('config_cache_factory')->setArguments([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,9 @@
<argument type="service" id="service_container" />
<argument type="tagged_iterator" tag="container.env_var_loader" />
</service>

<service id="monolog.http_client" class="Symfony\Contracts\HttpClient\HttpClientInterface">
<factory class="Symfony\Component\HttpClient\HttpClient" method="create" />
</service>
</services>
</container>