在Rails应用程序中记录所有方法调用

daf*_*tal 8 ruby rspec ruby-on-rails ruby-on-rails-3

有没有一种简单的方法来记录Rails应用程序中的所有方法调用?

我的主要用途是测试(和调试测试).我想拥有比堆栈跟踪更多的历史记录(例如,使用'-b'选项运行rspec时).

Uwe*_*der 18

这很容易做到.只需在脚本/服务器中添加5行代码:

#!/usr/bin/env ruby
set_trace_func proc {
  |event, file, line, id, binding, classname| 
  if event == "call" or event == "return" 
    printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
  end
}

require File.expand_path('../../config/boot',  __FILE__)
require 'commands/server'
Run Code Online (Sandbox Code Playgroud)

它在http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution中描述

您的应用程序将变得非常慢,您可能会获得比您想要的更多的输出.您可以轻松地在文件/类/函数名称上添加更多条件,以避免打印不需要的东西.