Skip to content

Commit 25e1cd2

Browse files
authored
Make it easier to enable the debug logger (#880)
* 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
1 parent 1b045d5 commit 25e1cd2

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

config/sentry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
// @see https://docs.sentry.io/product/sentry-basics/dsn-explainer/
1111
'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),
1212

13+
// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#logger
14+
// 'logger' => Sentry\Logger\DebugFileLogger::class, // By default this will log to `storage_path('logs/sentry.log')`
15+
1316
// The release version of your application
1417
// Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD'))
1518
'release' => env('SENTRY_RELEASE'),

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Sentry\Laravel\Http\SetRequestMiddleware;
2626
use Sentry\Laravel\Tracing\BacktraceHelper;
2727
use Sentry\Laravel\Tracing\ServiceProvider as TracingServiceProvider;
28+
use Sentry\Logger\DebugFileLogger;
2829
use Sentry\SentrySdk;
2930
use Sentry\Serializer\RepresentationSerializer;
3031
use Sentry\State\Hub;
@@ -50,6 +51,13 @@ class ServiceProvider extends BaseServiceProvider
5051
'controllers_base_namespace',
5152
];
5253

54+
/**
55+
* List of options that should be resolved from the container instead of being passed directly to the SDK.
56+
*/
57+
protected const OPTIONS_TO_RESOLVE_FROM_CONTAINER = [
58+
'logger',
59+
];
60+
5361
/**
5462
* List of features that are provided by the SDK.
5563
*/
@@ -116,6 +124,10 @@ public function register(): void
116124

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

127+
$this->app->singleton(DebugFileLogger::class, function () {
128+
return new DebugFileLogger(storage_path('logs/sentry.log'));
129+
});
130+
119131
$this->configureAndRegisterClient();
120132

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

289+
foreach (self::OPTIONS_TO_RESOLVE_FROM_CONTAINER as $option) {
290+
if (isset($options[$option]) && is_string($options[$option])) {
291+
$options[$option] = $this->app->make($options[$option]);
292+
}
293+
}
294+
277295
$clientBuilder = ClientBuilder::create($options);
278296

279297
// Set the Laravel SDK identifier and version
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Sentry\Laravel\Tests\Laravel;
4+
5+
use Sentry\Laravel\Tests\TestCase;
6+
use Sentry\Logger\DebugFileLogger;
7+
use Sentry\State\HubInterface;
8+
9+
class LaravelContainerConfigOptionsTest extends TestCase
10+
{
11+
public function testLoggerIsNullByDefault(): void
12+
{
13+
$logger = app(HubInterface::class)->getClient()->getOptions()->getLogger();
14+
15+
$this->assertNull($logger);
16+
}
17+
18+
public function testLoggerIsResolvedFromDefaultSingleton(): void
19+
{
20+
$this->resetApplicationWithConfig([
21+
'sentry.logger' => DebugFileLogger::class,
22+
]);
23+
24+
$logger = app(HubInterface::class)->getClient()->getOptions()->getLogger();
25+
26+
$this->assertInstanceOf(DebugFileLogger::class, $logger);
27+
}
28+
}

test/Sentry/Laravel/LaravelIntegrationsOptionTest.php renamed to test/Sentry/Laravel/LaravelIntegrationsConfigOptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Sentry\Integration\FatalErrorListenerIntegration;
1111
use Sentry\Laravel\Tests\TestCase;
1212

13-
class LaravelIntegrationsOptionTest extends TestCase
13+
class LaravelIntegrationsConfigOptionTest extends TestCase
1414
{
1515
protected function defineEnvironment($app): void
1616
{

0 commit comments

Comments
 (0)