如何在 rails 中禁用片段缓存的日志

How to disable logs for fragment caching in rails

您好,我有一个应用程序,其中不断地写入日志:

Read fragment views/locations/946-20150420170206 (0.1ms)
Read fragment views/locations/947-20150420170206 (0.1ms)
Read fragment views/locations/948-20150420170206 (0.1ms)
Read fragment views/locations/949-20150420170206 (0.1ms)
Read fragment views/locations/950-20150420170206 (0.1ms)
Read fragment views/locations/951-20150420170206 (0.1ms)
Read fragment views/locations/952-20150420170206 (0.1ms)
Read fragment views/locations/953-20150420170206 (0.1ms)
Read fragment views/locations/954-20150420170206 (0.1ms)
Read fragment views/locations/955-20150420170206 (0.1ms)
Read fragment views/locations/956-20150420170206 (0.1ms)
Read fragment views/locations/957-20150420170206 (0.1ms)
Read fragment views/locations/958-20150420170206 (0.1ms)
Read fragment views/locations/959-20150420170206 (0.1ms)
Read fragment views/locations/960-20150420170206 (0.1ms)
Read fragment views/locations/961-20150420170206 (0.1ms)
Read fragment views/locations/962-20150420170206 (0.1ms)
Read fragment views/locations/963-20150420170207 (0.1ms)
Read fragment views/locations/964-20150420170207 (0.1ms)
Read fragment views/locations/965-20150420170207 (0.1ms)
Read fragment views/locations/966-20150420170207 (0.1ms)
Read fragment views/locations/967-20150420170207 (0.1ms)
Read fragment views/locations/968-20150420170207 (0.1ms)
Read fragment views/locations/969-20150420170207 (0.1ms)
Read fragment views/locations/970-20150420170207 (0.1ms)
Read fragment views/locations/971-20150420170207 (0.1ms)
Read fragment views/locations/972-20150420170207 (0.1ms)

正在填充日志并使文件达到 GB 大小。所以我想禁用缓存读取和写入的日志,这样我就可以停止它。我尝试了各种方法:

module ActionView
  class LogSubscriber < ActiveSupport::LogSubscriber
    def logger
      @memoized_logger ||= Logger.new('/dev/null')
    end
  end
end

我将此文件保存在初始值设定项文件夹中,但没有帮助。我在初始化程序中尝试的第二件事是:

Rails.cache.silence!

但仍然没有任何改变。我也在 production.rb:

中尝试过这个
config.cache_store.silence!

但这引发了错误。我想可能没有定义 cache_store

我也在初始化程序中尝试了这个:

# Create logger that ignores messages containing “CACHE”
class CacheFreeLogger < ::Logger
  def debug(message, *args, &block)
    super unless message.include? 'Read fragment'
  end
end

# Overwrite ActiveRecord’s logger
ActiveRecord::Base.logger = ActiveSupport::TaggedLogging.new(
  CacheFreeLogger.new(STDOUT)) unless Rails.env.test?

仍然没有帮助。

我得到了另一个可能有帮助的答案,但我不知道如何使用它。这是 link:

How to disable logging for rails 4 caching

有人可以帮我禁用日志吗? 提前致谢。

您可以替换默认的 Rails 日志 formatter:

@logdevice = Rails.logger
                  .instance_variable_get(:@logger)
                  .instance_variable_get(:@log) # get rails logger

@formatter = @logdevice.formatter # store formatter value
@logdevice.formatter = proc do |severity, datetime, progname, message|
   @formatter.call(severity, datetime, progname, message) \
     unless message =~ /#{STOP_WORDS_REGEXP}/ # call unless there is a stop word
end

此代码可以放在您的应用程序中的任何位置。