Skip to navigation
2012 Apr 12

Integrating Sentry with PHP Error Reporting

When you have a site in production, errors are not something that you want your users to see. However, the importance of reporting these errors can not be overstated. If your errors aren't reported, how are you going to fix them afterall? Traditionally, PHP errors get dumped into the PHP error log on the server and then parsed at a later time so that the developer can fix the errors. These errors don't give you a ton of useful information though. That's where Sentry comes in.



Sentry is a realtime event logging and aggregation platform. Originally is was built by the kind folks over at Disqus to aggregate the errors that happened with their service. Realizing the platform's value to the development community, they open-sourced it.

Sentry is built on top of the Django framework, and integrates very easily with it. As a mostly PHP shop, we were pleasantly surprised to find out that Sentry's PHP integration was top-notch as well. For our initial testing, we opted to use instead of running our own server. The rest of this post will assume that you are too (it has a free version if you'd like to follow along).

So let's get this thing going. First off, you're going to need Raven-PHP. This is how your site is going to interact with the Sentry server. It can be installed as a package (PECL), or just included in your application. I chose the latter for my tests.

  1. Install Raven-PHP: If you go to the Raven-PHP GitHub page, you can down the entire project. Extract the raven-php/lib/Raven directory into your site's path.
  2. Register the auto-loader: This get's Raven set up so that it can attempt to communicate with the Sentry server.
  3. Create a client instance: Next, we need to tell Raven where to communicate. The DSN value tells Raven where to send the errors to.
    $dsn = "";
    $client = new Raven_Client();
  4. Override PHP's default error handler: The easiest and most comprehensive way to use Sentry is to override PHP's default error handler so that all of the errors get passed to it. You'll want todo this as early as possible in your application.
    $error_handler = new Raven_ErrorHandler($client);
    set_error_handler(array($error_handler, 'handleError'));
    set_exception_handler(array($error_handler, 'handleException'));
    A word of warning: This catches EVERYTHING. Even errors that normally don't get logged with full error reporting on. We modified the Raven source a little to only log notices, warnings, and fatals.

Designed and created by DDM Marketing & Communications.