Skip to content
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

Make it easier to enable the debug logger #880

Merged
merged 4 commits into from
Apr 11, 2024
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
3 changes: 3 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
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
Loading