Laravel

How to enable error logging using Sentry in Laravel

In a production environment, it is essential to get an instant alert if any error occurs in your Laravel application. If you are part of a big team, the alert should be passed on to the right person to resolve the issue.

Imagine if some error occurred in your Laravel application on the production environment, and after that, you have to go through the large file of error logs to find what happened at a given time for a particular user (it occurred to me so many times). It would be ideal to browse through the error history of your Laravel application right within your browser.

That’s where the Sentry comes into play. Sentry is a cloud-based error-tracking tool that comes with free and paid subscriptions. You can browse the complete list of features on their website at sentry.io. One of the most excellent features of Sentry is the fact that it’s super easy to integrate into your Laravel application. So let’s get started.
Step 1. Installation
Firstly, you must register an account on the Sentry website or use self-hosted Sentry and create a project. Creating an account and project in Sentry is very easy, and we will skip this process in our blog post. Once you have done that, come back here!

I will be using the latest release of Laravel, which is ^10.0. To create a new Laravel application, I will use the composer create-project command.

composer create-project laravel/laravel laravel-sentry

Now, we need to integrate Sentry into the Laravel application using the native package “sentry-laravel” via composer.

composer require sentry/sentry-laravel

After that, when the package “sentry-laravel” is installed, we will need to generate the config file for the package using the bellow command.

php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider"

When you create a new account on the Sentry, you should receive the DSN string (SENTRY_LARAVEL_DSN), which is like an API key. Next, we will add that key to our .env file

SENTRY_LARAVEL_DSN=https://[email protected]/4506138123456789

Sentry documentation for Laravel will advise you to hook into Laravel error capturing via an Exception handler.

I always set up the Sentry using the log channel, so I recommend you follow this approach as it will be more reliable.
To use the log channel, you must add a new channel to your config/logging.php configuration file.


    'channels' => [
        // ...
        'sentry' => [
            'driver' => 'sentry',
            'level'  => null,
            'bubble' => true,
        ],
    ],

You can see two extra configuration values in the above code snippet: level and bubble.
The level allows you to control the minimum error level sent to the Sentry. Laravel uses Monolog for handling errors so that you can look at the available levels in their documentation.
The bubble allows/prevents errors from being reported to other channels after Sentry handles the error.

Step 2. Configuring Channel

To configure your application to use the Sentry as a log channel, change the LOG_CHANNEL value below.

LOG_CHANNEL=sentry

From now all errors will be sent to Sentry.
In my application, I like to use the stack option, enabling our application to log errors to multiple channels. I use the daily and sentry as my stack because I first want to log errors to a local file and then send it to Sentry.
This configuration allows me to keep track of errors in a log file, and it will let me always see all log data if my Sentry uses more quota than I have for logging.
So, for my above approach, update the .env file to below.

LOG_CHANNEL=stack

and in the config/logging.php file, update the stack channel as below.


    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily', 'sentry'],
            'ignore_exceptions' => false,
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
            'replace_placeholders' => true,
        ],

        'sentry' => [
            'driver' => 'sentry',
            'level'  => null,
            'bubble' => true,
        ],
    ],

Step 3. Testing Error Logging

By now, we have integrated the Sentry into our Laravel application, and we should test if everything is okay. For this, the Sentry package provides an excellent command for testing the integration.
In your console, run the below command.

php artisan sentry:test

If everything is okay, you will see an output like below.

DSN discovered from Laravel config or `.env` file!
Sending test event...
Test event sent with ID: 38823b10ccdf483f86316ad4cbddb1e5

By the way, you will also see your new issue in your Sentry dashboard on the Sentry website for this test.

We can also test it by throwing an exception. For example, add the below route in your routes/web.php file and visit the route in the browser.

Route::get('/sentry-test', function () {
    throw new \Exception("Sentry Test Exception");
});

You will receive an email alert if you have set up that in the Sentry account area and will also see it on the Sentry dashboard like below.