Strategies for Rails Logging and Error Handling

Sometimes too much of a good thing (log info) is a bad thing, and that’s true with Rails default logging. Check out the gem ’lograge’. The big difference is that a single request will take a single line. To quote the README, instead of logs like this:

Started GET "/" for at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
  Rendered text template within layouts/application (0.0ms)
  Rendered layouts/_assets.html.erb (2.0ms)
  Rendered layouts/_top.html.erb (2.6ms)
  Rendered layouts/_about.html.erb (0.3ms)
  Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)

After installing lograge, you’ll have one line for the request:

method=GET path=/jobs/833552.json format=json controller=jobs action=show status=200 duration=58.33 view=40.43 db=15.26

The one issue with lograge is that the default configuration does not log request parameters, which can be useful for debugging. This blog post, How to add request parameters to lograge logs, addresses that shortcoming.

This is a companion discussion topic for the original entry at

Lograge is great if you want simple key-value logs. If you want a little more customization and different loggers,
check out Log4r or Logging gem. I wrote an introduction to Log4r on my blog to get you started:

I am not so sure about the email my error messages, because if there is a bug, it will spam your inbox. A LOT. There are services like airbrake and honeybadger that give you detailed information about the error and only alert you once per unique error.

Hey Justin!

I like your article about the topic, because every application developer should think about how to handle logging and error handling. I would recommend you take a look at error tracking services for production use, because:

* It will group the errors to similar error groups, so you won't get notified about the same error multiple times
* You see how often and at what time the errors occurred
* You get notifications to your Hipchat or Campfire account
* Error reporting of background jobs

Maybe you want to try out and send me your feedback (I'm the founder, but there are other services too).

Awesome article Justin! I really enjoyed the section on Utility.log_exception. What a fabulous method! I’d like to share a post that details an alternative to DIY error logging: Enjoy!

Thank for the post! Really tell me something important.