Skip to content

Pass logger from options to TransportFactory #555

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

Merged
merged 1 commit into from
Sep 14, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix: Test if `TracingStatement` exists before attempting to create the class alias, otherwise it breaks when opcache is enabled. (#552)
- Fix: Pass logger from `logger` config option to `TransportFactory` (#555)

## 4.2.2 (2021-08-30)

Expand Down
10 changes: 9 additions & 1 deletion src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension;
use Sentry\Serializer\RepresentationSerializer;
use Sentry\Serializer\Serializer;
use Sentry\Transport\TransportFactoryInterface;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -124,14 +125,21 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
->setPublic(false)
->setArgument(0, new Reference('sentry.client.options'));

$loggerReference = null === $config['logger']
? new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)
: new Reference($config['logger']);

$factoryBuilderDefinition = $container->getDefinition(TransportFactoryInterface::class);
$factoryBuilderDefinition->setArgument('$logger', $loggerReference);

$clientBuilderDefinition = (new Definition(ClientBuilder::class))
->setArgument(0, new Reference('sentry.client.options'))
->addMethodCall('setSdkIdentifier', [SentryBundle::SDK_IDENTIFIER])
->addMethodCall('setSdkVersion', [PrettyVersions::getVersion('sentry/sentry-symfony')->getPrettyVersion()])
->addMethodCall('setTransportFactory', [new Reference($config['transport_factory'])])
->addMethodCall('setSerializer', [$serializer])
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition])
->addMethodCall('setLogger', [null !== $config['logger'] ? new Reference($config['logger']) : new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
->addMethodCall('setLogger', [$loggerReference]);

$container
->setDefinition('sentry.client', new Definition(Client::class))
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" on-invalid="ignore" />
<argument type="service" id="Psr\Http\Message\ResponseFactoryInterface" on-invalid="ignore" />
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" on-invalid="ignore" />
<argument>null</argument>
<argument /> <!-- $logger -->
</service>

<service id="Sentry\State\HubInterface">
Expand Down
11 changes: 11 additions & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension;
use Sentry\Serializer\RepresentationSerializer;
use Sentry\Serializer\Serializer;
use Sentry\Transport\TransportFactoryInterface;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -245,6 +246,16 @@ public function testClientIsCreatedFromOptions(): void
$this->assertEquals($methodCalls[4][1][0]->getArgument(0), new Reference('sentry.client.options'));
}

public function testLoggerIsPassedToTransportFactory(): void
{
$container = $this->createContainerFromFixture('full');

$transportFactoryDefinition = $container->findDefinition(TransportFactoryInterface::class);
$logger = $transportFactoryDefinition->getArgument('$logger');

$this->assertSame('app.logger', $logger->__toString());
}

public function testErrorTypesOptionIsParsedFromStringToIntegerValue(): void
{
$container = $this->createContainerFromFixture('error_types');
Expand Down