将每个SQL查询记录到Rails 3中的数据库 [英] Log every SQL query to database in Rails 3
本文介绍了将每个SQL查询记录到Rails 3中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题是对问题的后续操作,我应该将该代码放在哪里?
This question is a follow up to this question, where should I place this code?
connection = ActiveRecord::Base.connection
class << connection
alias :original_exec :execute
def execute(sql, *name)
# try to log sql command but ignore any errors that occur in this block
# we log before executing, in case the execution raises an error
begin
file = File.open(RAILS_ROOT + "/log/sql.txt",'a'){|f| f.puts Time.now.to_s+": "+sql}
rescue Exception => e
;
end
# execute original statement
original_exec(sql, *name)
end
end
我尝试将其放置在模型中,但是发生的情况是,当我多次执行一些sql查询时,一旦返回"堆栈级别已达深"错误,就会发生这种情况.
I have tried placing it inside of the model, but what happens is when I execute some sql query more then once it returns "stack level is to deep" error.
推荐答案
将其放入config/initializers中.这很可能是因为每次在dev env中都重新加载类.这段代码只需要执行一次.
Put it in config/initializers. Most likely it's because of reloading classes each time in dev env. This code need to be executed only once though.
这篇关于将每个SQL查询记录到Rails 3中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文