2012-11-07 34 views
0

作爲此後續從post:我試圖記錄正在生成的所有查詢。我曾嘗試將它放在初始化程序中,並且只是直接在控制檯中運行它,並且執行似乎不會執行。Rails 2.3單獨記錄所有數據庫查詢

我曾嘗試以下:

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 
    raise "THROW 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 

然後運行

Person.last 

我仍然沒有錯誤。

回答

0

你不需要猴子補丁ActiveRecord來獲取日誌記錄(無論如何你在這裏做錯了)。我假設你想在生產中使用它,因爲在開發過程中所有查詢都已經記錄。只需設置這config/environments/production.rb

config.log_level = :debug 

或者,如果你想限制這SQL只查詢,沒有其他冗長,試試這個:

ActiveRecord::Base.logger.level = Logger::DEBUG 

或者,如果你的目標是編寫SQL查詢到一個專門的日誌文件,看看這個問題:Rails3 SQL logging output in a separate file

+0

第三個職位幫了我。謝謝 –

相關問題