37

I am working on rails project and I am trying to get exceptions to be logged to the rails log files. I know I can call logger.error $! to get the first line of the exception logged to the file. But, I want to get the entire trace stack logged as well. How do I log the entire trace back of an exception using the default rails logger?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Josh Moore
  • 13,338
  • 15
  • 58
  • 74

7 Answers7

38
logger.error $!.backtrace

Also, don't forget you can

rescue ErrorType => error_name

to give your error a variable name other than the default $!.

Ian Terrell
  • 10,667
  • 11
  • 45
  • 66
  • 1
    Am I missing something? logger.error only takes a single argument, so that code does not work... – Robin Clowers Feb 29 '12 at 22:39
  • 21
    This one doesn't work, use `logger.error e.message + "\n " + e.backtrace.join("\n ")` instead. – Jonas Fagundes May 05 '12 at 06:31
  • 3
    I could swear the multiple parameters worked 4 years ago, but maybe it didn't. I've updated it to just log the backtrace, which seems to be the most relevant aspect of the question; I trust the reader can figure out multiple logger calls are possible, ditto for string concatenation (or more Ruby-y, interpolation). – Ian Terrell May 05 '12 at 14:44
  • 1
    don't do join('\n') single quotes :p – James Jul 25 '14 at 15:55
18

The way rails does it is

137             logger.fatal(
138               "\n\n#{exception.class} (#{exception.message}):\n    " +
139               clean_backtrace(exception).join("\n    ") +
140               "\n\n"
141             )

248       def clean_backtrace(exception)
249         if backtrace = exception.backtrace
250           if defined?(RAILS_ROOT)
251             backtrace.map { |line| line.sub RAILS_ROOT, '' }
252           else
253             backtrace
254           end
255         end
256       end
Matt Burke
  • 3,306
  • 1
  • 22
  • 16
10

In later versions of Rails, simply uncomment the following line in RAIL_ROOT/config/initializers/backtrace_silencers.rb (or add this file itself if it's not there):

# Rails.backtrace_cleaner.remove_silencers!

This way you get the full backtrace written to the log on an exception. This works for me in v2.3.4.

mxgrn
  • 1,733
  • 16
  • 20
  • 1
    It's part of the documentation for Rails 3.2.3. See here: http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html – Rob May 25 '12 at 15:13
6

logger.error caller.join("\n") should do the trick.

Redbeard
  • 988
  • 5
  • 8
4

In Rails, ActionController::Rescue deals with it. In my application controller actions, i'm using method log_error from this module to pretty-format backtrace in logs:

def foo_action
  # break something in here
rescue
  log_error($!)
  # call firemen
end
Priit
  • 5,200
  • 2
  • 21
  • 20
3

Here's how I would do it:

http://gist.github.com/127708

Here's the ri documentation for Exception#backtrace:

http://gist.github.com/127710

Note that you could also use Kernel#caller, which gives you the full trace (minus the curent frame) as well.

http://gist.github.com/127709

Also - Note that if you are trying to catch all exceptions, you should rescue from Exception, not RuntimeError.

user52804
  • 319
  • 2
  • 5
1

You can also use ruby's default variables, like this:

logger.error "Your error message. Exception message:#{$!} Stacktrace:#{$@}"