5

我正在尝试禁用生产中缓存的日志记录。已成功让 SQL 停止记录查询,但缓存日志条目没有运气。生产日志中的示例行:

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

我不想禁用所有日志记录,因为我希望 logger.debug 语句显示在生产日志中。使用带有 Mysql 和 Apache 的 rails 3.2.1。有什么建议么?

4

3 回答 3

3

导轨 5.1

# config/initializers/active_record_logger.rb

class CacheFreeLogger < ActiveSupport::Logger
  def add(severity, message = nil, progname = nil, &block)
    return true if progname&.include? "CACHE"
    super
  end
end

ActiveRecord::Base.logger = CacheFreeLogger.new(STDOUT)
于 2018-07-07T22:42:04.343 回答
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?

原始帖子扩展了 Logger 而不是 TaggedLoggin 但它对我不起作用。

博客中建议了这种方法:http: //heliom.ca/blog/posts/disable-rails-cache-logging

于 2016-01-13T18:14:06.600 回答
-2

SO上有一些类似的问题(thisthisthis ),主题都相似。您可以尝试将其放入初始化程序文件中:

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

恢复记录器:

ActiveRecord::Base.logger = old_logger; nil # nil just to suppress some garbage 
于 2012-07-07T09:12:46.857 回答