跟踪 Rails 3 SQL 查询

发布于 2024-10-08 20:17:18 字数 357 浏览 4 评论 0原文

是否有任何适用于 Rails 3 的 gem 可以显示我的代码的哪一部分生成了哪个 SQL 查询

在 Rails 2.3 上有一个名为 query_trace 的插件,但它似乎不适用于 Rails 3,它会生成以下错误:

alias_method': undefined method `log_info' for class `ActiveRecord::ConnectionAdapters::AbstractAdapter' (NameError)

Is there any gem which works on Rails 3 that can show which part of my code generated which SQL query?

On Rails 2.3 there was plugin called query_trace, but it doesn't seem to work on Rails 3, it generates following error:

alias_method': undefined method `log_info' for class `ActiveRecord::ConnectionAdapters::AbstractAdapter' (NameError)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

强者自强 2024-10-15 20:17:18

QueryTrace 不能按原样工作,因为 Rails 3 特别是 ActiveRecord 领域进行了许多更改。

所以,我做了一些修改,让它像这样工作:

您只需要在提到的位置提供下面的 2 个文件。然后重新启动网络服务器。
在 SQL 之后,您应该在控制台(白色洋红色)中看到 Called from: 和日志文件

In /vendor/plugins/query_trace/lib/query_trace.rb

module QueryTrace
  def self.append_features(klass)
    super
    klass.class_eval do
      unless method_defined?(:log_info_without_trace)
        alias_method :log_info_without_trace, :sql
        alias_method :sql, :log_info_with_trace
      end
    end
  end

  def log_info_with_trace(event)
    log_info_without_trace(event)
    logger.debug("\e[1m\e[35m\e[1m\e[47mCalled from:\e[0m " + clean_trace(caller[2..-2]).join("\n "))
  end

  def clean_trace(trace)
    Rails.respond_to?(:backtrace_cleaner) ?
      Rails.backtrace_cleaner.clean(trace) :
      trace
  end
end

In <代码>/vendor/plugins/query_trace/init.rb

require 'query_trace'

class ::ActiveRecord::LogSubscriber
  include QueryTrace
end

QueryTrace doesn't work as-is because many changes were made in Rails 3 esp in the area of ActiveRecord.

So, hacking around, I made it work like this:

You just need the 2 files below in the locations mentioned. Then restart the web server.
After the SQL, you should see Called from: in a console (magenta on white) and log file

In /vendor/plugins/query_trace/lib/query_trace.rb

module QueryTrace
  def self.append_features(klass)
    super
    klass.class_eval do
      unless method_defined?(:log_info_without_trace)
        alias_method :log_info_without_trace, :sql
        alias_method :sql, :log_info_with_trace
      end
    end
  end

  def log_info_with_trace(event)
    log_info_without_trace(event)
    logger.debug("\e[1m\e[35m\e[1m\e[47mCalled from:\e[0m " + clean_trace(caller[2..-2]).join("\n "))
  end

  def clean_trace(trace)
    Rails.respond_to?(:backtrace_cleaner) ?
      Rails.backtrace_cleaner.clean(trace) :
      trace
  end
end

In /vendor/plugins/query_trace/init.rb

require 'query_trace'

class ::ActiveRecord::LogSubscriber
  include QueryTrace
end
家住魔仙堡 2024-10-15 20:17:18

query_trace 到rails3 的端口

https://gist.github.com/1137342

A port of query_trace to rails3

https://gist.github.com/1137342

℉服软 2024-10-15 20:17:18

active_record_query_trace 也可以做到这一点。

  1. 将以下内容添加到 Gemfile 中:
group :development do
  gem 'active_record_query_trace'
end
  1. 创建一个初始化程序(例如 config/initializers/active_record_query_trace.rb)以启用 gem。如果您想自定义 gem 的行为方式,您可以添加 docs 也到初始化程序。
if Rails.env.development?
 ActiveRecordQueryTrace.enabled = true
 # Optional: other gem config options go here
end
  1. 重新启动 Rails 开发服务器。

The active_record_query_trace also does that.

  1. Add the following to your Gemfile:
group :development do
  gem 'active_record_query_trace'
end
  1. Create an initializer such as config/initializers/active_record_query_trace.rb to enable the gem. If you want to customize how the gem behaves, you can add any combination of the options described in the docs to the initializer as well.
if Rails.env.development?
 ActiveRecordQueryTrace.enabled = true
 # Optional: other gem config options go here
end
  1. Restart the Rails development server.
等风也等你 2024-10-15 20:17:18

这里似乎有一个很好的分叉:

https://github.com/dolzenko/query_trace

请注意文档中的QUERY_TRACE常量似乎不起作用,但是QueryTrace.enable!确实(必须在初始化程序中,我尝试先将其放入 config/development.rb 中)

There appears to be a good fork here:

https://github.com/dolzenko/query_trace

Note that the QUERY_TRACE constant in the documentation doesn't seem to work, but the QueryTrace.enable! does (must be in an initializer, I tried putting it into config/development.rb first)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文