如何使用默认的 Rails 记录器记录 Ruby 异常的整个回溯?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/228441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:45:36  来源:igfitidea点击:

How do I log the entire trace back of a Ruby exception using the default Rails logger?

ruby-on-railsrubylogging

提问by Josh Moore

I am working on rails project and I am trying to get exceptions to be logged to the rails log files. I know I can call logger.error $!to get the first line of the exception logged to the file. But, I want to get the entire trace stack logged as well. How do I log the entire trace back of an exception using the default rails logger?

我正在处理 rails 项目,并且正在尝试将异常记录到 rails 日志文件中。我知道我可以调用logger.error $!以获取记录到文件中的异常的第一行。但是,我也想记录整个跟踪堆栈。如何使用默认的 rails 记录器记录整个异常回溯?

回答by Ian Terrell

logger.error $!.backtrace

Also, don't forget you can

另外,别忘了你可以

rescue ErrorType => error_name

to give your error a variable name other than the default $!.

给你的错误一个不是默认的变量名$!

回答by Matt Burke

The way rails does it is

rails 的做法是

137             logger.fatal(
138               "\n\n#{exception.class} (#{exception.message}):\n    " +
139               clean_backtrace(exception).join("\n    ") +
140               "\n\n"
141             )

248       def clean_backtrace(exception)
249         if backtrace = exception.backtrace
250           if defined?(RAILS_ROOT)
251             backtrace.map { |line| line.sub RAILS_ROOT, '' }
252           else
253             backtrace
254           end
255         end
256       end

回答by mxgrn

In later versions of Rails, simply uncomment the following line in RAIL_ROOT/config/initializers/backtrace_silencers.rb (or add this file itself if it's not there):

在更高版本的 Rails 中,只需在 RAIL_ROOT/config/initializers/backtrace_silencers.rb 中取消注释以下行(如果该文件不存在,则自行添加):

# Rails.backtrace_cleaner.remove_silencers!

This way you get the full backtrace written to the log on an exception. This works for me in v2.3.4.

通过这种方式,您可以在异常情况下将完整的回溯写入日志。这在 v2.3.4 中对我有用。

回答by Redbeard

logger.error caller.join("\n")should do the trick.

logger.error caller.join("\n")应该做的伎俩。

回答by Priit

In Rails, ActionController::Rescuedeals with it. In my application controller actions, i'm using method log_errorfrom this module to pretty-format backtrace in logs:

在 Rails 中,ActionController::Rescue处理它。在我的应用程序控制器操作中,我使用log_error此模块中的方法在日志中使用漂亮格式的回溯:

def foo_action
  # break something in here
rescue
  log_error($!)
  # call firemen
end

回答by user52804

Here's how I would do it:

这是我将如何做到的:

http://gist.github.com/127708

http://gist.github.com/127708

Here's the ri documentation for Exception#backtrace:

这是 Exception#backtrace 的 ri 文档:

http://gist.github.com/127710

http://gist.github.com/127710

Note that you could also use Kernel#caller, which gives you the full trace (minus the curent frame) as well.

请注意,您还可以使用 Kernel#caller,它也为您提供完整的跟踪(减去当前帧)。

http://gist.github.com/127709

http://gist.github.com/127709

Also - Note that if you are trying to catch allexceptions, you should rescue from Exception, not RuntimeError.

另外 - 请注意,如果您试图捕获所有异常,您应该从 Exception 中拯救,而不是 RuntimeError。

回答by user3223833

You can also use ruby's default variables, like this:

您还可以使用 ruby​​ 的默认变量,如下所示:

logger.error "Your error message. Exception message:#{$!} Stacktrace:#{$@}"