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 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)

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

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

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"));

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:


  • joshr696

    Thank you, very helpful.

  • Pon

    Where do you put this stuff? In start/global.php ?

    • Chris Duell

      I’ll give the typical vague laravel answer of “wherever you like” as long as the file is loaded, but yes, initially the easiest place to pop it is in the global.php file, inside the App::error section

      • Pon


  • Kuldeep Anand

    Can you help me with using Monologs’ Pushover handler in laravel.

    • Chris Duell

      I haven’t worked with Pushover, but I’d suggest looking into using something like this to get you going, and using the above procedures to guide

      • Kuldeep Anand

        I tried using the package on the link you provided but could not get it to work…

  • Abhinandan


  • Guest

    “Sentry is an open source error aggregation service” not anymore =(

  • Chris Bautista

    For those who want to integrate Raven / Sentry into their Laravel 5.1 installation, here is a library that makes it a breeze to do:

    • Chris Duell –

      nice work :)