Rails:记录异常的整个堆栈跟踪 [英] Rails: Logging the entire stack trace of an exception

查看:35
本文介绍了Rails:记录异常的整个堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找出记录堆栈跟踪的正确方法.我遇到了 this 链接指出 logger.error $!, $!.backtrace 是要走的路,但这对我不起作用 log_error> 是.根据文档,我不知道将第二个参数传递给 error 方法是如何工作的,因为 rails 使用的 ruby​​ 记录器只接受一个参数.

I have been trying to figure out the right way to log a stack trace. I came across this link which states that logger.error $!, $!.backtrace is the way to go but that does not work for me log_error does. As per documentation I do not see how passing a second argument to the error method would work anyway because the ruby logger that rails uses only accepts a single argument.

奇怪的是(或者可能不是)第二个论点被接受,没有任何口译员投诉.但是,我传递给它的任何内容都将被忽略.

Strangely (or maybe not) the second argument is accepted without any interpreter complaints. However anything that I pass to it is ignored.

谁能解释我遗漏了什么?对错误的第二个参数是什么以及它在吃什么有什么见解吗?

Can anyone explain what I am missing? Any insight into what the second argument to error is for and what is eating it?

推荐答案

如果您查看 ActiveSupport 中 BufferedLogger 类的源代码,您会看到第二个参数是progname".仅当第一个参数为 nil 并且您没有给它任何块或块返回非真值时才使用.

If you look at the source for the BufferedLogger class in ActiveSupport, you'll see that the second argument is 'progname'. This is used only when the first argument is nil and you have either given it no block or the block return a non-true value.

本质上,你不能使用第二个参数来输出额外的东西.

In essence, you can't use the second parameter to output additional stuff.

您想要做的事情更类似于:

What you want to do is something more akin to:

begin
  raise
rescue => e
  logger.error e.message
  logger.error e.backtrace.join("\n")
end

根据您的日志设置方式,最好遍历回溯的每一行并单独打印,因为某些记录器不输出换行符,在这种情况下,您可以执行以下操作:

Depending on how you have your logging setup, it might be better to iterate through each line of the backtrace and print it separately as certain loggers don't output newlines, in which case you'd do something like:

begin
  raise
rescue => e
  logger.error e.message
  e.backtrace.each { |line| logger.error line }
end

这篇关于Rails:记录异常的整个堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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