如何在 Rails 应用程序中选择性地跟踪方法及其文件访问? [英] How to selectively trace methods and their file access in a Rails app?

查看:46
本文介绍了如何在 Rails 应用程序中选择性地跟踪方法及其文件访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Rails 应用程序,我想重载或修改 Kernel::load 以便它打印出完整路径,如 puts "-> #{File.expand_path(File.dirname(__FILE__))}/#{File.basename(__FILE__)}" 用于加载的每个 Rails 应用文件.

For a Rails app, I would like to overload or modify Kernel::load such that it will print out the full path like puts "-> #{File.expand_path(File.dirname(__FILE__))}/#{File.basename(__FILE__)}" for each Rails app file that is loaded.

我该怎么做?我想进一步能够过滤,所以我猜我会在那里抛出一个正则表达式,以便只显示输出(当我不希望它非常冗长时),表明对文件的访问当前 Rails 应用根目录.

How would I do this? I would like to further be able to filter, so I am guessing I'd throw a regex on there to be able to only show output (when I don't want it to be grossly verbose) that indicates the access of files within the current rails app root.

无论如何,我希望有一个重载此方法的示例,以及在服务器启动时在 Rails 应用程序中执行此操作的适当位置.

At any rate, I'd appreciate an example of overloading this method and where the appropriate place to do this would be active in the Rails app on server start.

分辨率:

好吧,经过一番折腾并发现 set_trace_func 之后,我想出了以下内容:

Well okay after a bit of beating things around and discovering set_trace_func I came up with the following:

我按照 mtrace gem 中实现的内容将 set_trace_fun 集成到 development.rb 的标题中.我有很多调整要做,以过滤掉除我的应用程序目录之外的所有内容以及许多其他闲聊方法,但到目前为止似乎已经奏效.与修改有效集成的mtrace源码如下:

I integrated set_trace_fun into the header of development.rb along the lines of what was implemented in the mtrace gem. I had a lot of tweaking to do to filter out everything but my app directory as well as a number of other chatty methods, but it seems to be working out thus far. The mtrace source that is effectively integrated with modification is as follows:

mtrace_indent = 0
set_trace_func lambda {|event, file, line, id, binding, klass|
  case event
  when 'call'
    puts "\e[90m| " * mtrace_indent +
      "\e[35m#{klass}\e[90m#\e[36m#{id} \e[90m#{file}:#{line}\e[0m" +
      "#{'  ' * ([10 - mtrace_indent, 1].max)}\e[90mtxmt://open/?url=file://#{ File.expand_path(file) }&line=#{ line }\e[0m"
    mtrace_indent += 1
  when 'return'
    mtrace_indent -= 1
  end
  mtrace_indent = 0 if mtrace_indent < 0
}

如果您使用的不是 Mac,当然可以删除 txmt url 部分,但这确实很有帮助.

Of course the txmt url portions can be removed if you're not on a mac, but that is really quite helpful.

推荐答案

试试这个:

module Kernel
  alias_method :old_load, :load

  def load filename, wrap = false
    puts "-> #{filename}"
    old_load filename, wrap
  end
end

您可以将其放入 config/initializers,但对于您要执行的操作而言,这可能有点晚了.

You can put this into config/initializers, but that may be a little bit late for what you're trying to do.

显然,Rails init 进程 config/environment.rb 开始,所以如果你把这段代码放在那个文件的顶部,它应该是第一个加载的东西.

Apparently, Rails init process starts with config/environment.rb, so if you put this code at the top of that file, it should be the first thing loaded.

这篇关于如何在 Rails 应用程序中选择性地跟踪方法及其文件访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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