Skip to content

Commit

Permalink
Fix setting the configured PSR-3 logger on the transport factory (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Sep 14, 2021
1 parent ca318a6 commit 31edf15
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
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

0 comments on commit 31edf15

Please sign in to comment.