2012-07-07 40 views
5

我試圖禁用生產中的緩存日誌記錄。已成功讓SQL停止記錄查詢,但沒有緩存日誌條目的運氣。例如線生產日誌:如何在rails中禁用activerecord緩存日誌記錄

CACHE (0.0ms) SELECT `merchants`.* FROM `merchants` WHERE `merchants`.`id` = 1 LIMIT 1 

我不想禁用所有日誌,因爲我想logger.debug語句來生產日誌顯示出來。 在Mysql和Apache中使用rails 3.2.1。 有什麼建議嗎?

+1

我想知道怎麼做使用Rails 2.3.5 – Joelio 2012-08-23 19:58:43

回答

-1

有幾個問題(thisthis,this)像這樣在SO上都有類似的主題。你可以嘗試把這個在初始化文件:

old_logger = ActiveRecord::Base.logger 
ActiveRecord::Base.logger = nil 

恢復記錄:

ActiveRecord::Base.logger = old_logger; nil # nil just to suppress some garbage 
+4

你的答案是什麼關係'CACHE(0.0ms)'般項是一回事嗎? – jdoe 2012-07-07 09:39:39

2

我使用下面鏈接後提出的方法沉默的CACHE日誌。

我用一個封裝器替換了默認的ActiveRecod記錄器,該封裝器可以過濾掉包含不需要的文本的消息,即'CACHE'。

首先,創建內部config/initializers文件例如active_record.rb和文件中定義一個包裝類並替換有效記錄日誌如下面的代碼:

# Implementation of logger that ignores messages containing forbidden words 
# here 「CACHE」 and "Settings Load" 
class CacheFreeLogger < ActiveSupport::TaggedLogging 

    @@excluded = ['Settings Load','CACHE'] 

    def add(severity, message = nil, progname = nil, &block) 
    if message.nil? 
     if block_given? 
     message = block.call 
     else 
     message = progname 
     progname = nil #No instance variable for this like Logger 
     end 
    end 
    if severity > Logger::DEBUG || !(@@excluded.map{|e| message.include? e}.include?(true)) 
     @logger.add(severity, "#{tags_text}#{message}", progname) 
    end 
    end 
end 

#Replace the existing logger with the filtering one 
ActiveRecord::Base.logger = CacheFreeLogger.new(ActiveRecord::Base.logger) if Rails.env.development? 

原帖擴展記錄儀不TaggedLoggin但沒有爲我工作。

此方法建議在博客:http://heliom.ca/blog/posts/disable-rails-cache-logging

+0

這不適用於導軌5 – 2016-07-17 21:09:30