在Ruby中自动记录异常 [英] Automatically Logging Exceptions in Ruby

查看:139
本文介绍了在Ruby中自动记录异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个库或简单的方法来捕获在Ruby程序中抛出的异常并将其记录到一个文件?我查看了 log4r logger ,但两者上的文档不提供任何关于我将如何做的例子。如果这些信息有帮助,我会远程运行此程序,并丢失stdout和stderr的句柄。



您会推荐什么?

解决方案

如果你想在野外散步,请尝试:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆