获取 Ruby 异常的调用者的调用者
现在我们的应用程序正在 Heroku 上运行,为了节省重新启动和可能的延迟,应用程序被设计为缓存所有错误并将它们发送到 stderr,并仅提供陈旧的缓存,直到问题通过另一次推送解决。这可以防止应用程序被删除...
但是,日志不断地填充我们不想要的整个跟踪,因此我构建了一个小片段(部分从 Rails 中窃取)来解析调用者并仅发送该行,但是使用从救援中发送错误,我们得到了错误的线路,所以我想知道如何才能找到呼叫者或呼叫者,或者是否有更好的方法来处理这种情况。这是片段:
module StdErr
def error(message)
file = 'Unknown'
line = '0'
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ caller[1]
file = $1
line = $2
end
$stderr.puts "ERROR: #{message} on line #{line} of #{file}"
end
end
Right now our application is running on Heroku, and in order to save restarts and possible latency the application is designed to cache all errors and send them to stderr and just server a stale cache until the issue is resolved by another push. This prevents application takedown...
However, the logs keep filling up excessively with entire traces which we don't want, so I built a little snippet (that is partially stolen from Rails) to parse caller and only send that line, however with errors being sent from a rescue, we get the wrong line so I was wondering how I could get the caller or the caller or if there was a better way to handle this situation. Here is the snippet:
module StdErr
def error(message)
file = 'Unknown'
line = '0'
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ caller[1]
file = $1
line = $2
end
$stderr.puts "ERROR: #{message} on line #{line} of #{file}"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我明白了,你想引发一个异常,并从调用者的调用者开始进行回溯。这样做:
If I get it, you want to raise a exception with a backtrace that begins on the caller of the caller. Do it:
caller
返回一个数组,您可以通过查看后面的元素来更深入地了解调用堆栈(请注意,您正在查看caller[1]
)caller
returns an array and you can go deeper into the call stack by looking at it's later elements (note that you're looking atcaller[1]
)