sentry/sentry-laravel
Official Sentry SDK for Laravel. Automatically captures unhandled exceptions, performance data, and context from your app, sending issues and traces to Sentry for faster debugging and monitoring. Supports modern Laravel versions with simple Composer install.
Installation:
composer require sentry/sentry-laravel
Configure DSN:
php artisan sentry:publish --dsn=YOUR_DSN_HERE
This auto-generates config/sentry.php and updates .env with SENTRY_LARAVEL_DSN.
Enable Exception Handling:
Modify bootstrap/app.php to integrate Sentry:
->withExceptions(function (Exceptions $exceptions) {
Integration::handles($exceptions);
})
First Use Case: Capture an exception in a controller:
use function Sentry\captureException;
try {
// Risky operation
} catch (\Throwable $e) {
captureException($e);
}
.env for SENTRY_LARAVEL_DSN and APP_ENV (should be local/production).SENTRY_TRACES_SAMPLE_RATE=1.0 to ensure all traces are captured.Exception Handling:
Integration::handles().captureException($e) or Sentry\captureMessage() for custom events.queue:work in Sentry\startTransaction() for job tracing.Transaction Tracing:
Sentry\configureScope(function ($scope) {
$scope->setTransaction('custom-transaction-name');
});
Sentry\startTransaction('job:name') in job handles.Performance Monitoring:
Sentry\ViewEngineDecorator.config/sentry.php:
'sql_bindings' => true,
Logging:
LOG_CHANNEL=stack
LOG_STACK=sentry_logs,single
SENTRY_ENABLE_LOGS=true
SENTRY_LOG_LEVEL=warning
User Context:
Sentry\configureScope(function ($scope) {
$scope->setUser(['id' => auth()->id(), 'email' => auth()->user()->email]);
});
Sentry\configureScope(function ($scope) {
$scope->setTransaction('livewire:component-name');
});
SentryTracesSampleRate middleware to control sampling:
public function handle(ShouldQueue $job, Closure $next) {
return $next($job)->middleware(SentryTracesSampleRate::class);
}
Sentry\trace_metrics()->count('api.calls', 10, ['endpoint' => 'users']);
DSN Configuration:
SENTRY_LARAVEL_DSN is correct and not empty.php artisan config:clear after DSN changes.Environment Mismatch:
SENTRY_TRACES_SAMPLE_RATE=0.0 in non-production environments.Transaction Naming:
Sentry\configureScope.Log Level Conflicts:
SENTRY_LOG_LEVEL ignored if LOG_LEVEL is stricter.SENTRY_LOG_LEVEL=debug to override globally.SQL Bindings:
'sql_bindings' => false or limit to critical queries.Check Initialization:
php artisan sentry:check
Validates DSN, environment, and integrations.
Disable Integrations:
Temporarily disable auto-instrumentation in bootstrap/app.php:
Integration::handles($exceptions, false); // Disable auto-capture
Manual Event Testing:
Sentry\captureException(new \Exception('Test event'));
Sentry\captureMessage('Test message', 'test-level');
Custom Integrations:
Sentry\Integration for framework-specific hooks (e.g., Livewire, Octane).getSessionKey() for custom session storage.Scope Modifiers:
Sentry\configureScope(function ($scope) {
$scope->setTag('custom_tag', 'value');
});
Before/After Hooks:
Sentry\beforeSendEvent() to modify payloads:
Sentry\beforeSendEvent(function ($event) {
$event['extra']['custom_data'] = 'value';
return $event;
});
Metrics Sampling:
SENTRY_METRICS_SAMPLE_RATE (0.0–1.0) to balance performance and data volume.config/sentry.php to override settings per environment:
'sample_rate' => env('SENTRY_SAMPLE_RATE', config('sentry.sample_rate', 1.0)),
SENTRY_RELEASE in .env or:
Sentry\setRelease('v1.0.0');
Sentry\configureScope(function ($scope) {
$scope->ignoreException(function (\Throwable $e) {
return $e instanceof \Some\IgnoredException;
});
});
How can I help you explore Laravel packages today?