Skip to content

Commit

Permalink
Make it easier to enable the debug logger (#880)
Browse files Browse the repository at this point in the history
* Add a default instance of the file logger to the container

* Resolve the `logger` option from the container

* Add tests

* Add commented out comment in config for logger
  • Loading branch information
stayallive committed Apr 11, 2024
1 parent 1b045d5 commit 25e1cd2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/sentry.php
Expand Up @@ -10,6 +10,9 @@
// @see https://docs.sentry.io/product/sentry-basics/dsn-explainer/
'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),

// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#logger
// 'logger' => Sentry\Logger\DebugFileLogger::class, // By default this will log to `storage_path('logs/sentry.log')`

// The release version of your application
// Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD'))
'release' => env('SENTRY_RELEASE'),
Expand Down
18 changes: 18 additions & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Expand Up @@ -25,6 +25,7 @@
use Sentry\Laravel\Http\SetRequestMiddleware;
use Sentry\Laravel\Tracing\BacktraceHelper;
use Sentry\Laravel\Tracing\ServiceProvider as TracingServiceProvider;
use Sentry\Logger\DebugFileLogger;
use Sentry\SentrySdk;
use Sentry\Serializer\RepresentationSerializer;
use Sentry\State\Hub;
Expand All @@ -50,6 +51,13 @@ class ServiceProvider extends BaseServiceProvider
'controllers_base_namespace',
];

/**
* List of options that should be resolved from the container instead of being passed directly to the SDK.
*/
protected const OPTIONS_TO_RESOLVE_FROM_CONTAINER = [
'logger',
];

/**
* List of features that are provided by the SDK.
*/
Expand Down Expand Up @@ -116,6 +124,10 @@ public function register(): void

$this->mergeConfigFrom(__DIR__ . '/../../../config/sentry.php', static::$abstract);

$this->app->singleton(DebugFileLogger::class, function () {
return new DebugFileLogger(storage_path('logs/sentry.log'));
});

$this->configureAndRegisterClient();

$this->registerFeatures();
Expand Down Expand Up @@ -274,6 +286,12 @@ protected function configureAndRegisterClient(): void
$options['before_send_transaction'] = $wrapBeforeSend($options['before_send_transaction'] ?? null);
}

foreach (self::OPTIONS_TO_RESOLVE_FROM_CONTAINER as $option) {
if (isset($options[$option]) && is_string($options[$option])) {
$options[$option] = $this->app->make($options[$option]);
}
}

$clientBuilder = ClientBuilder::create($options);

// Set the Laravel SDK identifier and version
Expand Down
28 changes: 28 additions & 0 deletions test/Sentry/Laravel/LaravelContainerConfigOptionsTest.php
@@ -0,0 +1,28 @@
<?php

namespace Sentry\Laravel\Tests\Laravel;

use Sentry\Laravel\Tests\TestCase;
use Sentry\Logger\DebugFileLogger;
use Sentry\State\HubInterface;

class LaravelContainerConfigOptionsTest extends TestCase
{
public function testLoggerIsNullByDefault(): void
{
$logger = app(HubInterface::class)->getClient()->getOptions()->getLogger();

$this->assertNull($logger);
}

public function testLoggerIsResolvedFromDefaultSingleton(): void
{
$this->resetApplicationWithConfig([
'sentry.logger' => DebugFileLogger::class,
]);

$logger = app(HubInterface::class)->getClient()->getOptions()->getLogger();

$this->assertInstanceOf(DebugFileLogger::class, $logger);
}
}
Expand Up @@ -10,7 +10,7 @@
use Sentry\Integration\FatalErrorListenerIntegration;
use Sentry\Laravel\Tests\TestCase;

class LaravelIntegrationsOptionTest extends TestCase
class LaravelIntegrationsConfigOptionTest extends TestCase
{
protected function defineEnvironment($app): void
{
Expand Down

0 comments on commit 25e1cd2

Please sign in to comment.