如何获得code触发查询行了? [英] How to get the line of code that triggers a query?
本文介绍了如何获得code触发查询行了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法(珠玑,插件或别的东西)在轨3.2知道code这行触发数据库查询? 例如,在我的日志我有:
is there a way (a gem, a plugin or something else) in rails 3.2 to know which line of code triggers a database query? For example in my log I have:
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
我怎么能知道code触发查询行了? THX ...
How can I know the line of code that triggers the query? Thx...
推荐答案
我发现这个解决方案:
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)
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
end
end
在一些初始化添加 QueryTrace.enable!
这篇关于如何获得code触发查询行了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文