我开始研究现有的rails项目,在rails服务器输出中有这些额外的行:
User Load (10.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'miles@milestshirts.com' LIMIT 1
\_ Called from: app/controllers/application_controller.rb:10:in `block in <class:ApplicationController>'
基本上对于我的rails应用程序中的每个SQL查询,它添加了一个调用它的行。 我似乎无法在Gemfile中找到它,因为它有超过150个宝石。
答案 0 :(得分:1)
看起来像Active Record Query Trace:
启用后,每个查询源都将记录为:
IntuitAccount Load (1.2ms) SELECT "intuit_accounts".* FROM "intuit_accounts" WHERE "intuit_accounts"."user_id" = 20 LIMIT 1
Called from:
app/views/users/edit.html.haml:78:in `block in _app_views_users_edit_html_haml___1953197429694975654_70177901460360'
app/views/users/edit.html.haml:16:in `_app_views_users_edit_html_haml___1953197429694975654_70177901460360'
答案 1 :(得分:0)
我在项目lib文件夹中找到它。如果有人在这里是一个简单的代码:
module QueryTrace
def self.enable!
::ActiveRecord::LogSubscriber.send(:include, self)
end
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)
begin
log_info_without_trace(event)
trace_log = Rails.backtrace_cleaner.clean(caller).first
if trace_log && event.payload[:name] != 'SCHEMA'
logger.debug(" \\_ \e[33mCalled from:\e[0m " + trace_log)
end
rescue
end
end
end