hakanersu
8/29/2016 - 6:04 PM

Loading Laravel Sentry only for production

Loading Laravel Sentry only for production

<?php

// App/Providers/AppServiceProvider.php

...
public function register()
{
    if ($this->app->environment('local')) {

        // Dev only service providers

    } else if ($this->app->environment('production')) {

        $this->app->register(SentryLaravelServiceProvider::class);

    }
}


// App/Exceptions/Handler.php

...
public function report(Exception $e)
{
    if (app()->environment('production') && $this->shouldReport($e)) {

        app('sentry')->captureException($e);

    }

    parent::report($e);
}

Loading Laravel Sentry package only for production

In the case that you don't want development errors making their way to Sentry for error tracking, you can use the code below to ensure that Sentry will only record exceptions when in production.

First, instead of loading the SentryLaravelServiceProvider in the config/app.php providers array, we will load it in our App/Providers/AppServiceProvider.php. This allows us to check the environment before loading the appropriate service providers.

Seconly, in our App/Exceptions/Handler.php we again check for the production environment before trying to capture the exception using Sentry. This second step prevents Sentry from trying to catch an exception when it isn't bound in the container.