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?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- I want to trace logs using a Macro multi parameter
- How to specify memcache server to Rack::Session::M
相关文章
- how do I log requests and responses for debugging
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
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.
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):
This way you get the full backtrace written to the log on an exception. This works for me in v2.3.4.
You can also use ruby's default variables, like this:
Also, don't forget you can
to give your error a variable name other than the default
$!
.In Rails,
ActionController::Rescue
deals with it. In my application controller actions, i'm using methodlog_error
from this module to pretty-format backtrace in logs:The way rails does it is