Automatically Logging Exceptions in Ruby
Is there a library or easy way to catch exceptions thrown in a Ruby program and log it to a file? I've looked over log4r and logger, but the docs on both don't provide any examples on how I would do this. I run this program remotely and lose handles to stdout and stderr, if that information helps at all.
What would you recommend?
If you want to take a walk on the wild side, try this:
class Exception
alias real_init initialize
def initialize(*args)
real_init *args
# log the error (self) or its args here
end
end
This will intercept the creation of new exception objects at the moment of creation.
Rescue from Exception
. Something like this probably makes sense:
begin
# run your code here ..
rescue Exception => exception
# logger.error(...) ....
raise exception
end
This will log the exception, and re-raise it so that the application actually raises an error in addition to the logging.
exception
is an instance of Exception
, take a look at the docs for information about what you can do with this object (such as accessing the backtrace).
如果您正在运行Rails应用程序,那么Exception Notification插件非常方便。
链接地址: http://www.djcxy.com/p/25856.html上一篇: 从Mysql2 :: Error中拯救
下一篇: 在Ruby中自动记录异常