Lugo Labs

Simple exception monitoring in Ruby on Rails

If something goes wrong with our live application, we want to know, right? Then we can have a look at the error message, its stack, and go directly to that line of code that is the problem and fix it quickly. And we surely need to fix it quickly, since the bug is been spotted by a user (hahaaaaa!!).

There are many paid services that can notify about errors and more, New Relic and Airbrake come to mind. But we can start with a free gem, that could be sufficient in many cases.

The gem I use is exception_notification (formerly Exception Notifier Plugin for Rails. Let's add it to our Gemfile:

rb
# Gemfile

gem 'exception_notification', github: 'smartinez87/exception_notification'

We also need a mail service (like Postfix) installed in your server. We can also use Gmail (free for up to 500 messages a day) or transactional mail services like Mailgun that offer up to 10 thousand free emails per month.

I have Postfix installed in one of my servers, so the configuration for it would be:

text
# config/environments/production.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "127.0.0.1",
  :port                 => 25,
  :enable_starttls_auto => false,
  :authentication       => 'plain'
}

Note how we added this to the production.rb environment file. In the same file we add the configuration for the exception_notification:

text
# config/environments/production.rb

config.middleware.use ExceptionNotification::Rack, :email => {
  :email_prefix         => "[YOUR APPLICATION error] ",
  :sender_address       => %{"Exception Notifier" <error@YOUR_APPLICATION_DOMAIN.com>},
  :exception_recipients => %w{YOUR EMAIL(S)}
}

Naturally replace the bits in capitals above as per your case.

And that's it. Happy monitoring!