KEMBAR78
# Laravel | Sentry for Laravel ## [Prerequisites](https://docs.sentry.io/platforms/php/guides/laravel.md#prerequisites) * You need a [Sentry account](https://sentry.io/signup/) and project * Your Laravel application needs to run on PHP 7.2 or later * Your Laravel version needs to be 11.x or higher * Read one of these guides if you use an [older version](https://docs.sentry.io/platforms/php/guides/laravel/other-versions.md) or need [Lumen-specific instructions](https://docs.sentry.io/platforms/php/guides/laravel/other-versions/lumen.md) ## [Install](https://docs.sentry.io/platforms/php/guides/laravel.md#install) Install the `sentry/sentry-laravel` package: ```bash composer require sentry/sentry-laravel ``` Enable capturing unhandled exception to report to Sentry by making the following change to your `bootstrap/app.php`: `bootstrap/app.php` ```php withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // }) ->withExceptions(function (Exceptions $exceptions) { Integration::handles($exceptions); }) ->create(); ``` Alternatively, you can configure Sentry as a [Laravel Log Channel](https://docs.sentry.io/platforms/php/guides/laravel/usage.md#log-channels). ## [Configure](https://docs.sentry.io/platforms/php/guides/laravel.md#configure) Configure the Sentry DSN with this command: ```shell php artisan sentry:publish --dsn=https://examplePublicKey@o0.ingest.sentry.io/0 ``` It creates the config file (`config/sentry.php`) and adds the `DSN` to your `.env` file. `.env` ```shell SENTRY_LARAVEL_DSN=https://examplePublicKey@o0.ingest.sentry.io/0 ``` In order to receive stack trace arguments in your errors, make sure to set `zend.exception_ignore_args: Off` in your php.ini ## [Verify](https://docs.sentry.io/platforms/php/guides/laravel.md#verify) ### [Verify With Artisan](https://docs.sentry.io/platforms/php/guides/laravel.md#verify-with-artisan) You can test your configuration using the provided `sentry:test` artisan command: ```shell php artisan sentry:test ``` ### [Verify With Code](https://docs.sentry.io/platforms/php/guides/laravel.md#verify-with-code) You can verify that Sentry is capturing errors in your Laravel application by creating a route that will throw an exception: `routes/web.php` ```php 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. ## [Tracing](https://docs.sentry.io/platforms/php/guides/laravel.md#tracing) 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` ```shell # 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](https://docs.sentry.io/platforms/php/guides/laravel/configuration/filtering.md#using-sampling-to-filter-transaction-events). Performance data is transmitted using a new event type called "transactions", which you can learn about in [Distributed Tracing](https://docs.sentry.io/product/sentry-basics/tracing/distributed-tracing.md#traces-transactions-and-spans). ## [Local Development and Testing](https://docs.sentry.io/platforms/php/guides/laravel.md#local-development-and-testing) 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. ## [Set up Sentry through Forge](https://docs.sentry.io/platforms/php/guides/laravel.md#set-up-sentry-through-forge) If you're using Laravel's Forge platform to provision and deploy your PHP application, you can create a Sentry organization through [Forge](https://forge.laravel.com/docs/integrations/sentry.html).