Using Sentry for log tracking with Laravel
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
Damn, this is a hard topic to search for. Any time I went searching for resources on getting the log management system called Sentry to work with Laravel, I was instead overwhelmed with resources on the user authentication system called Sentry. The trick is to search for Raven with Laravel, Raven being the package that helps Laravel to communicate with the Sentry service. After much sifting to find relevant resources (and ignoring any packages that hook Laravel into Sentry for you, as a package for that seems like unnecessary overheard to me), I've got it up and running!
First, you'll need to install the
raven-php package. Add the following to your
composer.json file and run
Next, you'll need to register the handler. Luckily Laravel uses Monolog for logging, and Monolog has a handler that supports Raven out of the box! Add the following to your
global.php file, or pop it in a server provider if that's your thing. Note that I first check that it's the production environment before registering the handler, as errors that occur in development or staging don't really need to make their way to Sentry (and of course you should probably have a different environment set up on Sentry if you want to track other environments anyway).
Of course, make sure to swap out the URL for your Sentry endpoint (it will be in the project settings in Sentry).
For extra credit, you can also limit the types of logs that get to Sentry. The second parameter you pass to
RavenHandler will be the minimum error type that will be sent.
This, for example, will only send ERRORS and higher log types.