在 Ruby 中获取当前堆栈跟踪而不引发异常 [英] Get current stack trace in Ruby without raising an exception

查看:51
本文介绍了在 Ruby 中获取当前堆栈跟踪而不引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Rails 3 应用程序中记录当前的回溯 (stacktrace), 不会发生异常.知道怎么做吗?

I want to log the current backtrace (stacktrace) in a Rails 3 app without an exception occurring. Any idea how?

我为什么要这个?我正在尝试跟踪 Rails 查找模板时进行的调用,以便我可以选择要覆盖的流程的一部分(因为我想更改我的特定子类控制器的视图路径).

Why do I want this? I'm trying to trace the calls that are made when Rails looks for a template so that I can choose a part of the process to override (because I want to change the view path for a particular subclassed controller of mine).

我想从文件中调用它:gems\actionpack-3.2.3\lib\action_dispatch\middleware\templates\rescues\missing_template.erb.我知道这不是最佳实践,但我知道它位于搜索模板的堆栈下游.

I'd like to call it from the file: gems\actionpack-3.2.3\lib\action_dispatch\middleware\templates\rescues\missing_template.erb. I know that's not best practice, but I know it's downstream of the stack from where the search for templates occurs.

推荐答案

您可以使用 Kernel#caller:

You can use Kernel#caller:

# /tmp/caller.rb

def foo 
  puts caller # Kernel#caller returns an array of strings
end

def bar 
  foo 
end

def baz 
  bar 
end

baz

输出:

caller.rb:8:in `bar'
caller.rb:12:in `baz'
caller.rb:15:in `<main>'

这篇关于在 Ruby 中获取当前堆栈跟踪而不引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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