Musings of a startup founder

Musings of a startup founder


Chris Duell, co-founder @ elev.io

Chris Duell
Author

CEO & Co-founder of elev.io

Share


elevio

Twitter


Using custom monolog handlers in Laravel

Being able to neatly and efficiently track and view errors in your app is crucial to keeping things flowing nicely for your users. This article takes a look at…

Chris DuellChris Duell

Being able to neatly and efficiently track and view errors in your app is crucial to keeping things flowing nicely for your users. This article takes a look at how you can use Monolog to help you stay in control of your errors.

I’m using Laravel 4 for a project, which provides some cool error logging by simply using something like this, which is in start/global.php

App::error(function(Exception $exception, $code) { Log::error($exception); });

Because earlier in the file, useDailyFiles() is being called to setup file logging, whenever an error occurs the details are logged to file, easy as that. But that only uses part of the power of monolog.

The monolog package provides quite a few other handlers to be able to send the error data to other locations and services to help you stay on top of things and collate the errors.

One particular option, is to use sentry. Sentry is an open source error aggregation service. Simply put, it allows you to send the exception information to it, and it will contact you via whichever method suits you best (see the available plugins) including email. So when an error occurs, you’ll get an email, but more importantly, the system will collate all similar errors together so you can see which are once off issues, and which are reoccurring and how often, so you know what’s more important to fix.

Setting things up though for monolog to send this information to sentry isn’t well documented in one place, so I had to some searching through the monolog docs, and the code itself to see where to do what. There could be a better approach to this, but it seems to work well for me.

First we need to get a handle on monolog itself from Laravels wrapping log writer class

$monolog = Log::getMonolog();

Then we need to setup the handler we want to make use of, in this case we’re going to use the Raven client (which sends data to sentry), and initialise it with the location it will send the data to.

Then add this handler to the list of items monolog sends the error information to, along with the minimum level of error we want to send.

You’ll need to get your endpoint from your sentry projects settings > API keys page, it will be something like https://xxxxxxx:yyyyyyyyy@app.getsentry.com/6487

$client = new Raven_Client(YOUR_ENDPOINT_HERE); $handler = new Monolog\Handler\RavenHandler($client, Monolog\Logger::ERROR); $monolog->pushHandler($handler);

If you add that in to your start/global.php file and an error occurs, the data will be sent to your sentry account, however there’s a problem, the message is prefixed with the current time and date, which means that no message is unique and so won’t be collated like we want. To do this, we need to override the handlers message format before adding it to monolog like this:

$client = new Raven_Client(YOUR_ENDPOINT_HERE); $handler = new Monolog\Handler\RavenHandler($client, Monolog\Logger::ERROR); $handler->setFormatter(new Monolog\Formatter\LineFormatter("%message% %context% %extra%\n")); $monolog->pushHandler($handler);

Now whenever an error occurs it will be sent to sentry, and each type of error will be collated together so you’ll see a count of each error, something like this:

Sentry

Chris Duell
Author

Chris Duell

CEO & Co-founder of elev.io

Comments