Skip to content

Add the logger config option to ease setting a PSR-3 logger to debug the SDK #538

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
Jul 20, 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 @@ -6,6 +6,7 @@
- Add the `sentry_trace_meta()` Twig function to print the `sentry-trace` HTML meta tag (#510)
- Make the list of commands for which distributed tracing is active configurable (#515)
- Introduce `TracingDriverConnection::getWrappedConnection()` (#536)
- Add the `logger` config option to ease setting a PSR-3 logger to debug the SDK (#538)

## 4.1.4 (2021-06-18)

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('If this value is not provided, the SDK will try to read it from the SENTRY_DSN environment variable. If that variable also does not exist, the SDK will just not send any events.')
->end()
->booleanNode('register_error_listener')->defaultTrue()->end()
->scalarNode('logger')
->info('The service ID of the PSR-3 logger used to log messages coming from the SDK client. Be aware that setting the same logger of the application may create a circular loop when an event fails to be sent.')
->defaultNull()
->end()
->scalarNode('transport_factory')
->info('The service ID of the transport factory used by the default SDK client.')
->defaultValue(TransportFactoryInterface::class)
Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use LogicException;
use Psr\Log\NullLogger;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Integration\IgnoreErrorsIntegration;
Expand Down Expand Up @@ -129,7 +130,8 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
->addMethodCall('setSdkVersion', [PrettyVersions::getVersion('sentry/sentry-symfony')->getPrettyVersion()])
->addMethodCall('setTransportFactory', [new Reference($config['transport_factory'])])
->addMethodCall('setSerializer', [$serializer])
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition]);
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition])
->addMethodCall('setLogger', [null !== $config['logger'] ? new Reference($config['logger']) : new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);

$container
->setDefinition('sentry.client', new Definition(Client::class))
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/schema/sentry-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<xsd:attribute name="register-error-listener" type="xsd:boolean" />
<xsd:attribute name="transport-factory" type="xsd:string" />
<xsd:attribute name="dsn" type="xsd:string" />
<xsd:attribute name="logger" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="options">
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void
{
$expectedBundleDefaultConfig = [
'register_error_listener' => true,
'logger' => null,
'transport_factory' => 'Sentry\\Transport\\TransportFactoryInterface',
'options' => [
'integrations' => [],
Expand Down
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/php/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
$container->loadFromExtension('sentry', [
'dsn' => 'https://[email protected]/0',
'transport_factory' => 'App\\Sentry\\Transport\\TransportFactory',
'logger' => 'app.logger',
'options' => [
'integrations' => ['App\\Sentry\\Integration\\FooIntegration'],
'default_integrations' => false,
Expand Down
10 changes: 10 additions & 0 deletions tests/DependencyInjection/Fixtures/php/logger_service_not_set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @var ContainerBuilder $container */
$container->loadFromExtension('sentry', [
'logger' => null,
]);
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<sentry:config
dsn="https://[email protected]/0"
transport-factory="App\Sentry\Transport\TransportFactory"
logger="app.logger"
>
<sentry:options default-integrations="false"
send-attempts="1"
Expand Down
10 changes: 10 additions & 0 deletions tests/DependencyInjection/Fixtures/xml/logger_service_not_set.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd">

<sentry:config logger="null" />
</container>
1 change: 1 addition & 0 deletions tests/DependencyInjection/Fixtures/yml/full.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sentry:
dsn: https://[email protected]/0
transport_factory: App\Sentry\Transport\TransportFactory
logger: app.logger
options:
integrations:
- App\Sentry\Integration\FooIntegration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sentry:
logger: ~
12 changes: 12 additions & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Jean85\PrettyVersions;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Sentry\ClientInterface;
use Sentry\Integration\IgnoreErrorsIntegration;
use Sentry\Options;
Expand Down Expand Up @@ -231,6 +232,7 @@ public function testClentIsCreatedFromOptions(): void
$this->assertDefinitionMethodCallAt($methodCalls[0], 'setSdkIdentifier', [SentryBundle::SDK_IDENTIFIER]);
$this->assertDefinitionMethodCallAt($methodCalls[1], 'setSdkVersion', [PrettyVersions::getVersion('sentry/sentry-symfony')->getPrettyVersion()]);
$this->assertDefinitionMethodCallAt($methodCalls[2], 'setTransportFactory', [new Reference('App\\Sentry\\Transport\\TransportFactory')]);
$this->assertDefinitionMethodCallAt($methodCalls[5], 'setLogger', [new Reference('app.logger')]);

$this->assertSame('setSerializer', $methodCalls[3][0]);
$this->assertInstanceOf(Definition::class, $methodCalls[3][1][0]);
Expand Down Expand Up @@ -369,6 +371,16 @@ public function testConsoleTracingListenerIsConfiguredWhenTracingIsEnabled(): vo
$this->assertSame(['foo:bar', 'bar:foo'], $container->getDefinition(TracingConsoleListener::class)->getArgument(1));
}

public function testLoggerOptionFallbackToNullLoggerIfNotSet(): void
{
$container = $this->createContainerFromFixture('logger_service_not_set');
$clientDefinition = $container->findDefinition(ClientInterface::class);
$factory = $clientDefinition->getFactory();
$methodCalls = $factory[0]->getMethodCalls();

$this->assertDefinitionMethodCallAt($methodCalls[5], 'setLogger', [new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
}

private function createContainerFromFixture(string $fixtureFile): ContainerBuilder
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
Expand Down