I want to log the current backtrace (stacktrace) in a Rails 3 app without an exception occurring. Any idea how?
我希望在Rails 3应用程序中记录当前回溯(stacktrace),而不会出现异常。任何想法如何?
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).
我为什么要这个?我正在尝试跟踪Rails寻找模板时发出的调用,这样我就可以选择要覆盖的进程的一部分(因为我想要更改我的一个特定子类控制器的视图路径)。
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.
我想从文件中调用它:gems\动作包-3.2.3\lib\action_dispatch\中间件\模板挽救\missing_template.erb。我知道这不是最佳实践,但我知道它位于堆栈的下游,从搜索模板发生的地方。
3 个解决方案
#1
158
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
Output:
输出:
caller.rb:8:in `bar'
caller.rb:12:in `baz'
caller.rb:15:in `<main>'
#2
7
Try using
试着用
Thread.current.backtrace
#3
3
I use this to show a custom error page when exception are raised.
当出现异常时,我使用它显示一个自定义错误页面。
rescue_from Exception do |exception|
logger.error exception.class
logger.error exception.message
logger.error exception.backtrace.join "\n"
@exception = exception
# ExceptionNotifier::Notifier.exception_notification env, @exception
respond_to do |format|
if [AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownAction].include?(exception.class)
format.html { render :template => "errors/404", :status => 404 }
format.js { render :nothing => true, :status => 404 }
format.xml { render :nothing => true, :status => 404 }
elsif exception.class == CanCan::AccessDenied
format.html {
render :template => "errors/401", :status => 401 #, :layout => 'application'
}
# format.js { render :json => { :errors => [exception.message] }, :status => 401 }
# format.js { render :js => 'alert("Hello 401")' }
format.js { render :template => 'errors/401.js.erb' }
else
ExceptionNotifier::Notifier.exception_notification(env, exception).deliver
format.html { render :template => "errors/500", :status => 500 } #, :layout => 'im2/application' }
# format.js { render :nothing => true, :status => 500 }
format.js { render :template => 'errors/500.js.erb' }
end
end
end
#1
158
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
Output:
输出:
caller.rb:8:in `bar'
caller.rb:12:in `baz'
caller.rb:15:in `<main>'
#2
7
Try using
试着用
Thread.current.backtrace
#3
3
I use this to show a custom error page when exception are raised.
当出现异常时,我使用它显示一个自定义错误页面。
rescue_from Exception do |exception|
logger.error exception.class
logger.error exception.message
logger.error exception.backtrace.join "\n"
@exception = exception
# ExceptionNotifier::Notifier.exception_notification env, @exception
respond_to do |format|
if [AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownAction].include?(exception.class)
format.html { render :template => "errors/404", :status => 404 }
format.js { render :nothing => true, :status => 404 }
format.xml { render :nothing => true, :status => 404 }
elsif exception.class == CanCan::AccessDenied
format.html {
render :template => "errors/401", :status => 401 #, :layout => 'application'
}
# format.js { render :json => { :errors => [exception.message] }, :status => 401 }
# format.js { render :js => 'alert("Hello 401")' }
format.js { render :template => 'errors/401.js.erb' }
else
ExceptionNotifier::Notifier.exception_notification(env, exception).deliver
format.html { render :template => "errors/500", :status => 500 } #, :layout => 'im2/application' }
# format.js { render :nothing => true, :status => 500 }
format.js { render :template => 'errors/500.js.erb' }
end
end
end