Laravel 8.x, 9.x and 10.x
Learn about using Sentry with Laravel 8.x, 9.x and 10.x.
Laravel is supported using a native package: sentry-laravel.
This guide is for Laravel 8.x, 9.x and 10.x. We also provide instructions for the latest Laravel as well as Lumen-specific instructions.
Install the sentry/sentry-laravel package:
composer require sentry/sentry-laravel
composer require sentry/sentry-laravel
Enable capturing unhandled exception to report to Sentry by making the following change to your App/Exceptions/Handler.php:
App/Exceptions/Handler.phpuse Sentry\Laravel\Integration;
public function register(): void
{
$this->reportable(function (Throwable $e) {
Integration::captureUnhandledException($e);
});
}
use Sentry\Laravel\Integration;
public function register(): void
{
$this->reportable(function (Throwable $e) {
Integration::captureUnhandledException($e);
});
}
If you also want to send structured logs to Sentry, see the Logs guide.
Configure the Sentry DSN with this command:
php artisan sentry:publish --dsn=___PUBLIC_DSN___
php artisan sentry:publish --dsn=___PUBLIC_DSN___
It creates the config file (config/sentry.php) and adds the DSN to your .env file.
.envSENTRY_LARAVEL_DSN=___PUBLIC_DSN___
SENTRY_LARAVEL_DSN=___PUBLIC_DSN___
In order to receive stack trace arguments in your errors, make sure to set zend.exception_ignore_args: Off in your php.ini
You can test your configuration using the provided sentry:test artisan command:
php artisan sentry:test
php artisan sentry:test
You can verify that Sentry is capturing errors in your Laravel application by creating a route that will throw an exception:
routes/web.phpRoute::get('/debug-sentry', function () {
throw new Exception('My first Sentry error!');
});
Route::get('/debug-sentry', function () {
throw new Exception('My first Sentry error!');
});
Visiting this route will trigger an exception that will be captured by Sentry.
Set traces_sample_rate in config/sentry.php or SENTRY_TRACES_SAMPLE_RATE in your .env to a value greater than 0.0. Setting a value greater than 0.0 will enable Tracing, null (the default) will disable Tracing.
.env# Be sure to lower this value in production otherwise you could burn through your quota quickly.
SENTRY_TRACES_SAMPLE_RATE=1.0
# Be sure to lower this value in production otherwise you could burn through your quota quickly.
SENTRY_TRACES_SAMPLE_RATE=1.0
The example configuration above will transmit 100% of captured traces. Be sure to lower this value in production or you could use up your quota quickly.
You can also be more granular with the sample rate by using the traces_sampler option. Learn more in Using Sampling to Filter Transaction Events.
If your queued jobs generate more traces than your web requests, you can also lower the sample rate for specific jobs with Sentry\Laravel\Jobs\Middleware\SentryTracesSampleRate. See Laravel Options.
Performance data is transmitted using a new event type called "transactions", which you can learn about in Distributed Tracing.
When Sentry is installed in your application, it will also be active when you are developing or running tests.
You most likely don't want errors to be sent to Sentry when you are developing or running tests. To avoid this, set the DSN value to null to disable sending errors to Sentry.
You can also do this by not defining SENTRY_LARAVEL_DSN in your .env or by defining it as SENTRY_LARAVEL_DSN=null.
If you do leave Sentry enabled when developing or running tests, it's possible for it to have a negative effect on the performance of your application or test suite.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").