Legacy Laravel integration guide

Add BugSnag to your legacy Laravel applications.

This integration supports Laravel 4 (and Laravel 5 running PHP 5.4 or lower). For newer applications, see the updated Laravel integration guide.

Installation

  1. Add bugsnag/bugsnag-laravel to your composer.json as follows:

    $ composer require "bugsnag/bugsnag-laravel:^1.7"
    
  2. Register our service provider in providers array in config/app.php:

    'providers' => [
        ...
        'Bugsnag\BugsnagLaravel\BugsnagLaravelServiceProvider',
    ],
    
  3. Register the BugSnag alias in the aliases array in config/app.php:

    'aliases' => [
        ...
        'Bugsnag' => 'Bugsnag\BugsnagLaravel\BugsnagFacade',
    ],
    

Basic configuration

Create a config/bugsnag.php configuration file as follows:

$ php artisan config:publish bugsnag/bugsnag-laravel

To associate your application with a project in your BugSnag dashboard, you’ll need to set your Integration API Key in app/config/packages/bugsnag/bugsnag-laravel/config.php:

return array(
  'api_key' => 'YOUR_API_KEY_HERE'
);

You can find this key immediately after creating a new project from your BugSnag dashboard, or later on your project’s settings page.

If you’d like to configure BugSnag further, check out the configuration options reference.

Reporting unhandled exceptions

Laravel 4

At this point, BugSnag should be installed and configured, and any unhandled exceptions will be automatically detected and should appear in your BugSnag dashboard.

Laravel 5

You can ensure that all unhandled exceptions are sent to BugSnag by using our ExceptionHandler.

Remove the following line from app/Exceptions/Handler.php:

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

And replace it with the BugSnag ExceptionHandler:

use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler;

Reporting handled exceptions

Reporting handled exceptions can be accomplished as follows:

try {
    // Some potentially crashy code
} catch (Exception $e) {
    Bugsnag::notifyException($e);
}

When reporting handled exceptions, it’s often helpful to send us custom diagnostic data or to adjust the severity of particular errors. For more information, see reporting handled errors.

Sending diagnostic data

Automatically captured diagnostics

BugSnag will automatically capture the following data for every exception:

  • A full stacktrace
  • Request information, including ip, headers, URL, HTTP method, and HTTP params
  • Session and cookie data
  • Release stage (production, beta, staging, etc)
  • Hostname

Custom diagnostics

It is often helpful to send us additional application-specific diagnostic data. This can be accomplished as follows:

Bugsnag::setBeforeNotifyFunction(function ($error) {
    $error->setMetaData([
      'account' => [
          'name' => 'Acme Co.',
          'paying_customer' => true,
      ]
    ]);
});

For more information, see the customizing error reports reference.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your BugSnag dashboard.

If you are Laravel’s built-in authentication, we will attempt to automatically capture information about the currently authenticated user and send this to your BugSnag dashboard.

If you are using a different authentication system, you can set this information as follows by putting the following in the boot method of a service provider:

Bugsnag::setBeforeNotifyFunction(function ($error) {
    $error->setUser([
        'id' => '123456',
        'name' => 'Leeroy Jenkins',
        'email' => 'leeeeroy@jenkins.com',
    ]);
});

For more information, see configuration options.

Tracking deploys

By sending your source revision or application version to us when you deploy a new version of your app, you’ll be able to see which deploy each error was introduced in, or seen in.

See our Build API guide for more details.

Next steps