Laravel Error Tracking

Typical installation time: 5 minutes

Hi there! You've found Honeybadger's guide to Laravel error tracking. Once installed, Honeybadger will automatically report errors wherever they may happen:

  • During a web request
  • In a scheduled command
  • In a background task
  • When a process crashes

On this page:

Installation

First, install the honeybadger-laravel package via composer:

composer require honeybadger-io/honeybadger-laravel

Then, add the following configuration to your .env file. You can find your project API key on your Project Settings page in Honeybadger:

HONEYBADGER_API_KEY="your-project-api-key"

Note: This package uses Laravel's package discovery to register the service provider and facade to the framework. If you are using an older version of Laravel or do not use package discovery, you may need to manually register those components.


Next, add Honeybadger reporting to app/Exceptions/Handler.php:

public function report(Exception $exception)
{
    if (app()->bound('honeybadger') && $this->shouldReport($exception)) {
        app('honeybadger')->notify($exception, app('request'));
    }

    parent::report($exception);
}

Finally, create the Honeybadger configuration file (config/honeybadger.php):

php artisan vendor:publish --provider="Honeybadger\HoneybadgerLaravel\HoneybadgerServiceProvider" --tag="config"

To test your configuration, try running the honeybadger:test artisan command:

php artisan honeybadger:test

You should now have an error report in Honeybadger!

Identifying Users

The following middleware will add the user's auth identifier context automatically. This middleware should be applied after any middleware that handles authentication:

// app/Http/Kernel.php
protected $middlewareGroup = [
    'web' => [
        \Honeybadger\HoneybadgerLaravel\Middleware\UserContext::class,
    ]
];

Adding Context

Context can be added by either the provided Facade or by resolving from the service container.

Facade

Honeybadger::context('key', $value);

DI Resolution

use Honeybadger\Honeybadger;

public function __construct(Honeybadger $honeybadger)
{
    $honeybadger->context('key', $value);
}

Helper Resolution

use Honeybadger\Honeybadger;

public function __construct()
{
    app('honeybadger')->context('key', $value);
    app(Honeybadger::class)->context('key', $value)
}

Check-Ins

honeybadger-laravel integrates with Honeybadger's Check-In feature to help you know when your scheduled tasks and background jobs go missing or silently fail.

Scheduled Command

This method is great for ensuring your application is up and running.

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule->command('honeybadger:checkin 1234')->everyFiveMinutes();
}

After a Scheduled Command

This method is great for making sure specific scheduled commands are running on time.

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule->command('inspire')
        ->everyFiveMinutes()
        ->thenPingHoneybadger('1234');
}

Command

> php artisan honeybadger:checkin 1234