Lumen integration guide
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
Installation
Section titled “Installation”First, install the honeybadger-laravel package via composer:
composer require honeybadger-io/honeybadger-laravel…and add the following line to bootstrap/app.php under the “Register Service
Providers” section:
$app->register(\Honeybadger\HoneybadgerLaravel\HoneybadgerServiceProvider::class);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, run the honeybadger:install artisan command:
php artisan honeybadger:install [Your project API key]The honeybadger:install command does three things:
- Adds
HONEYBADGER_API_KEYto.envand.env.example - Creates Honeybadger’s
config/honeybadger.phpconfiguration file - Sends a test notification to your Honeybadger project
If everything is set up correctly, you should now have an error report in Honeybadger!
Identifying users
Section titled “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
Section titled “Adding context”Context can be added by either the provided Facade or by resolving from the service container.
Facade
Section titled “Facade”Honeybadger::context('key', $value);DI resolution
Section titled “DI resolution”use Honeybadger\Honeybadger;
public function __construct(Honeybadger $honeybadger){ $honeybadger->context('key', $value);}Helper resolution
Section titled “Helper resolution”use Honeybadger\Honeybadger;
public function __construct(){ app('honeybadger')->context('key', $value); app(Honeybadger::class)->context('key', $value)}Using Honeybadger as a logger
Section titled “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”:
'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.
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:
$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:
'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single', 'honeybadger'], 'ignore_exceptions' => false, ], // ... ],