Lumen Exception & Error Tracking

Typical installation time: 5 minutes

Hi there! You've found Honeybadger's guide to Lumen error and exception 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:

bash
composer require honeybadger-io/honeybadger-laravel

...and add the following line to bootstrap/app.php under the "Register Service Providers" section:

php
$app->register(\Honeybadger\HoneybadgerLaravel\HoneybadgerServiceProvider::class);

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

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

Finally, run the honeybadger:install artisan command:

bash
php artisan honeybadger:install [Your project API key]

The honeybadger:install command does three things:

  1. Adds HONEYBADGER_API_KEY to .env and .env.example
  2. Creates Honeybadger's config/honeybadger.php configuration file
  3. Sends a test notification to your Honeybadger project

If everything is set up correctly, you should now have an error report in Honeybadger!

Note: The default config for Lumen won't report errors to Honeybadger on local and testing environments. You can change that with the report_data option.

Identifying Users

Honeybadger automatically captures details about the current logged-in user, as well as the controller and method name. No extra configuration needed. We only capture the user ID, so no sensitive information is transmitted.

When an error occurs, you'll see an Affected Users section on your dashboard, where we'll list the user IDs and how many times they encountered the error.

Adding Context

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

Facade

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

DI Resolution

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

Helper Resolution

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

Using Honeybadger as a Logger

If you prefer, you can also use Honeybadger as a log channel in your Lumen app. To do this, you'll need to register a custom channel in your config/logging.php, making use of the HoneybadgerLogDriver.

If you don't have a config/logging.php file, you can create one by copying the contents of the one embedded in Lumen.

Once you've done that, you can add a custom channel called "honeybadger":

php
config/logging.php
'channels' => [ // ... 'honeybadger' => [ 'driver' => 'custom', 'via' => Honeybadger\HoneybadgerLaravel\HoneybadgerLogDriver::class, 'name' => 'honeybadger' ], ],

Now you can write log messages as normal with Lumen's log facade, and they'll show up on your Honeybadger dashboard.

php
Log::channel('honeybadger')->info('An info message'); Log::channel('honeybadger')->('An info message with context data', ["some-key" => "some-value"]); Log::channel('honeybadger')->error('An error message');

If you include an exception context item in your error messages, we'll automatically format them for easy viewing:

php
$e = new \Exception('Something happened'); Log::channel('honeybadger')->error('An error message', ['exception' => $e]);

You can also add the custom channel to your default stack so you can automatically have exceptions logged to Honeybadger as well:

php
config/logging.php
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'honeybadger'], 'ignore_exceptions' => false, ], // ... ],