记录 Rails 应用程序中的所有方法调用 [英] Logging all method calls in a Rails app

查看:48
本文介绍了记录 Rails 应用程序中的所有方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以记录 Rails 应用中的所有方法调用?

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

解决方案

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

#!/usr/bin/env rubyset_trace_func 过程{|事件、文件、行、id、绑定、类名|if event == "call" or event == "return"printf "%8s %s:%-2d %10s %8s\n",事件,文件,行,id,类名结尾}需要 File.expand_path('../../config/boot', __FILE__)需要命令/服务器"

http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution

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

Is there an easy way to log all method calls in a Rails app?

My main use for this would be in testing (and in debugging tests). I want to have more of a history than a stacktrace provides (for instance, when running rspec with the '-b' option).

解决方案

It's easy to do. Just add 5 lines of code into your script/server:

#!/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'

It's described at http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution

Your application will become quite slow and you might get more output than you want. You can easily add more conditions on file/class/function names to avoid printing unwanted stuff.

这篇关于记录 Rails 应用程序中的所有方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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