Search code examples
ruby-on-railsruby-on-rails-3exceptionruby-on-rails-3.1

Ruby on Rails 3.1 - Logging exception in the database


what is the most robust way to have each exception logged in the database in rails 3.1 automatically (using Rails middleware probably)?

I know that it might sound like a standard question, but most of the answer I found so far was either a bit old (rails-2) or suggesting a local handling.

I watched http://railscasts.com/episodes/277-mountable-engines and Ryan is suggesting to use

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
    if payload[:exception]
      name, message = *payload[:exception]
      Uhoh::Failure.create!(:message => message)
    end
end

But he is also saying

The solution we’ll use for this isn’t very efficient but it’s simple and it will work for our case.

Is there a better alternative?

I have few exception classes, and I am handling the exception in the controller itself, but it would be nice to have them logged in a specific table of the database for analysis.

I come across the gem exception_logger (https://github.com/defunkt/exception_logger), but this gem seems a bit outdated (?).


Solution

  • For seemingly outdated gems such as exception_logger, I find it helpful to check out other users' forks of the gem using GitHub's network feature. You'll usually find that someone has updated or continued to maintain the gem even though the original author has not. For example, the network for exception_logger is quite active, especially this particular fork by QuBiT.

    Check out The Ruby Toolbox - Exception Notification for a list of widely used "exception notification" tools. Unfortunately most of them are a bit outdated, but the technique described above for finding more active forks applies as well.

    If you aren't opposed to a paid solution, Airbrake and Exceptional both work well (although Airbrake's new pricing structure make them quite prohibitive for a small project; Exceptional is reasonably priced).