按名称记录调试日志中的每个方法调用

时间:2012-10-15 17:38:02

标签: ruby-on-rails ruby methods

我试图调试一些不起作用的测试,我遇到了一些问题。当我运行我的测试并将关键消息输出到日志时,我正在拖尾test.log但由于所有交织的回调和钩子我不确定哪个模型调用哪个SQL哪个SQL:)

我写了一些东西来记录方法名称:

def log_mname
  caller[0]=~/`(.*?)'/  # note the first quote is a backtick
  Rails.logger.debug '=' * 80
  Rails.logger.debug ">> #{Time.now} - #{$1}"
  Rails.logger.debug '=' * 80
end

效果很好BUT要求我在模型中的每个方法中添加log_mname,这是不现实的。我想要的是在我的rails应用程序中添加一个简单的行或块,这让我基本上可以“记录调用日志时调用所有方法。”

我尝试在模型顶部使用ruby的set_trace_func,但它没有用。我也不想在每个模型中添加内容,而是在test / debug.rb环境文件等中添加内容。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你要问的概念被称为反思;如果你有兴趣阅读更多相关信息。

要回答您的问题,您希望知道它的名称的方法内的__method__会将方法名称作为符号返回

$ irb
irb(main):001:0> def qwerty
irb(main):002:1> __method__
irb(main):003:1> end
=> nil
irb(main):004:0> qwerty
=> :qwerty
irb(main):005:0> 

这适用于Ruby 1.8.7

修改

以上是打印方法名称。

要动态显示方法调用,我会使用ActiveSupport的#constantize与set_trace_func显示调用跟踪混合。

# in your test environment initializer file
require 'active_support/inflector/inflections'
MODELS_TO_WATCH = Dir.entries("#{Rails.root}/app/models/").
                      gsub(".rb", ""). # strip all extensions from filenames
                      capitalize!. # no reason for the bang, just saving space
                    # constantize # make "User" string into User model, for e.g.
# if you constantize, remove `to_s` from `classname` in the proc below
# the version that worked for me was without constantizing but I figure both 
# variations yield the same result

class_eval do |c| # Replace this with a "class DummyClass" for proper sandboxing
  set_trace_func proc { |event, id, classname|
    if event == "call" && MODELS_TO_WATCH.include?(classname.to_s)
      puts "called #{classname}'s #{id}"
    end
  }
end

注意! set_trace_func leech 功能。它将锁定你的Ruby进程,直到进程被杀死(许多IRB的死亡可以证明这一点)。我还没有找到撤消set_trace_func的方法。没有引入的条件,它有一个令人讨厌的打印输出;这可能看起来像错误,但它们不是。

这就是我建议将其放入测试初始化​​程序的原因;并且很可能在虚拟课堂中!这样,当您在开发,生产或您已设置的任何其他环境中重新启动Rails应用程序时,此hack不会影响它。

必须在课堂上下文中进行评估。我发现它是否在实例上下文中进行了求值,因此instance_eval,它会打印执行Ruby程序时发生的每个事件,除了被调用的函数。