在Ruby中自动记录异常 [英] Automatically Logging Exceptions in Ruby
本文介绍了在Ruby中自动记录异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您会推荐什么?
解决方案
如果你想在野外散步,请尝试:
class Exception
alias real_init initialize
def initialize(* args)
real_init * args
#记录错误(self)或其args这里
end
end
这将拦截创建时创建新的异常对象。 / p>
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.
这篇关于在Ruby中自动记录异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文