Jens Segers on

Laravel error logging with Sentry

Decent error logging is one of the most important things when it comes to building applications. It is so unprofessional when a client calls you about a bug and you have no idea what he is talking about. Wouldn't you rather be able to tell him "We know about the issue, and we are already working on it"?


Logging to a plain text file is just not enough, there's more to an exception than just the error message. You need to be able to track which user triggered the exception, the session information at that point, the current deploy version, the browser and operating system, the GET/POST parameters, and the list goes on.

I have tried a couple of error tracking services, but the one that stood out for me was Sentry. It has support for over 10 programming languages and most importantly, it has a free open-source version that you can host yourself. They also have a free limited pricing plan if you want to try it out before you either pay for their cloud solution or if you want to host it yourself.


Once you have signed up for Sentry and added your project, you will get a DSN that you can use to push your errors to their platform. The integration for your application is fairly simple; for plain PHP projects you can use their PHP client, or if you are using Laravel you can use my package which automatically collects additional Laravel context data:

composer require jenssegers/raven

When that is done, add the service provider to your config/app.php:


You can configure your DSN by either using the RAVEN_DSN environment variable or by adding it to Laravel's config/services.php file:

'raven' => [
    'dsn'   => 'your-raven-dsn',
    'level' => 'debug'

Instead of having to use a separate facade to speak to, the service provider adds a log listener to Laravel's internal logger. So whenever you want to send either an exception or a debug message to Sentry, you can simply use the Log facade:

catch (Exception $e)

Since Laravel 5, we have this great central exception handler which catches all uncaught exceptions. To make sure that these are sent to Sentry, all that we have to do is to modify the report method in app/Exceptions/Handler.php:

public function report(Exception $e)
    if ($this->shouldReport($e))

That's it. Now, whenever an error occurs, Laravel's exception handler will catch it and send it to Sentry with additional context information about the user's session, application environment, server, and IP. If you wish to add custom context data, you can pass it as a second argument:

\Log::debug('Event triggered', [
    'foo' => 'bar',
    'user' => $user

I hope you enjoyed reading this post and see how an error logging service such as Sentry can save you a lot of troubles when trying to locate a bug in your application.


Tweet about this blog post and you will appear below!