From 471b9e252621b3967e575b8a57ce51af98429307 Mon Sep 17 00:00:00 2001 From: Stefano Arlandini Date: Tue, 20 Jul 2021 13:00:08 +0200 Subject: [PATCH] Add the `logger` config option to ease setting a PSR-3 logger to debug the SDK (#538) --- CHANGELOG.md | 1 + src/DependencyInjection/Configuration.php | 4 ++++ src/DependencyInjection/SentryExtension.php | 4 +++- src/Resources/config/schema/sentry-1.0.xsd | 1 + tests/DependencyInjection/ConfigurationTest.php | 1 + tests/DependencyInjection/Fixtures/php/full.php | 1 + .../Fixtures/php/logger_service_not_set.php | 10 ++++++++++ tests/DependencyInjection/Fixtures/xml/full.xml | 1 + .../Fixtures/xml/logger_service_not_set.xml | 10 ++++++++++ tests/DependencyInjection/Fixtures/yml/full.yml | 1 + .../Fixtures/yml/logger_service_not_set.yml | 2 ++ tests/DependencyInjection/SentryExtensionTest.php | 12 ++++++++++++ 12 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/DependencyInjection/Fixtures/php/logger_service_not_set.php create mode 100644 tests/DependencyInjection/Fixtures/xml/logger_service_not_set.xml create mode 100644 tests/DependencyInjection/Fixtures/yml/logger_service_not_set.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index d9704cd5..a8dc1762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 6741bf84..2fd7ae3c 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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) diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index f707ae1d..01655d7f 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -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; @@ -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)) diff --git a/src/Resources/config/schema/sentry-1.0.xsd b/src/Resources/config/schema/sentry-1.0.xsd index 0f05cf5b..d5e3bbf7 100644 --- a/src/Resources/config/schema/sentry-1.0.xsd +++ b/src/Resources/config/schema/sentry-1.0.xsd @@ -17,6 +17,7 @@ + diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 87011025..5f73cfde 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -20,6 +20,7 @@ public function testProcessConfigurationWithDefaultConfiguration(): void { $expectedBundleDefaultConfig = [ 'register_error_listener' => true, + 'logger' => null, 'transport_factory' => 'Sentry\\Transport\\TransportFactoryInterface', 'options' => [ 'integrations' => [], diff --git a/tests/DependencyInjection/Fixtures/php/full.php b/tests/DependencyInjection/Fixtures/php/full.php index 72405535..0211c62d 100644 --- a/tests/DependencyInjection/Fixtures/php/full.php +++ b/tests/DependencyInjection/Fixtures/php/full.php @@ -8,6 +8,7 @@ $container->loadFromExtension('sentry', [ 'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0', 'transport_factory' => 'App\\Sentry\\Transport\\TransportFactory', + 'logger' => 'app.logger', 'options' => [ 'integrations' => ['App\\Sentry\\Integration\\FooIntegration'], 'default_integrations' => false, diff --git a/tests/DependencyInjection/Fixtures/php/logger_service_not_set.php b/tests/DependencyInjection/Fixtures/php/logger_service_not_set.php new file mode 100644 index 00000000..b48a9d63 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/logger_service_not_set.php @@ -0,0 +1,10 @@ +loadFromExtension('sentry', [ + 'logger' => null, +]); diff --git a/tests/DependencyInjection/Fixtures/xml/full.xml b/tests/DependencyInjection/Fixtures/xml/full.xml index c63e1ae5..67a7e52a 100644 --- a/tests/DependencyInjection/Fixtures/xml/full.xml +++ b/tests/DependencyInjection/Fixtures/xml/full.xml @@ -9,6 +9,7 @@ + + + + + diff --git a/tests/DependencyInjection/Fixtures/yml/full.yml b/tests/DependencyInjection/Fixtures/yml/full.yml index aa472a56..eb7ea1a0 100644 --- a/tests/DependencyInjection/Fixtures/yml/full.yml +++ b/tests/DependencyInjection/Fixtures/yml/full.yml @@ -1,6 +1,7 @@ sentry: dsn: https://examplePublicKey@o0.ingest.sentry.io/0 transport_factory: App\Sentry\Transport\TransportFactory + logger: app.logger options: integrations: - App\Sentry\Integration\FooIntegration diff --git a/tests/DependencyInjection/Fixtures/yml/logger_service_not_set.yml b/tests/DependencyInjection/Fixtures/yml/logger_service_not_set.yml new file mode 100644 index 00000000..02ded00d --- /dev/null +++ b/tests/DependencyInjection/Fixtures/yml/logger_service_not_set.yml @@ -0,0 +1,2 @@ +sentry: + logger: ~ diff --git a/tests/DependencyInjection/SentryExtensionTest.php b/tests/DependencyInjection/SentryExtensionTest.php index 2d502d03..de241ecc 100644 --- a/tests/DependencyInjection/SentryExtensionTest.php +++ b/tests/DependencyInjection/SentryExtensionTest.php @@ -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; @@ -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]); @@ -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([