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

Ruby on rails 如何在rails中禁用片段缓存日志,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,您好,我有一个应用程序,其中日志由以下人员编写: 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.1m

您好,我有一个应用程序,其中日志由以下人员编写:

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
但仍然没有改变。我也在
production.rb中尝试过这个:

config.cache_store.silence!
但这带来了一个错误。我想可能没有定义
cache\u-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?
#创建忽略包含“缓存”的消息的记录器
类CacheFreeLogger<::Logger
def调试(消息、*args和块)
超级邮件,包括吗读取片段'
结束
结束
#覆盖ActiveRecord的记录器
ActiveRecord::Base.logger=ActiveSupport::TaggedLogging.new(
CacheFreeLogger.new(STDOUT))除非Rails.env.test?
仍然没有帮助

我得到了另一个可能有用的答案,但我不知道如何使用这个。以下是链接:

有人能帮我禁用这个日志吗。
提前感谢。

您可以替换默认的
Rails
log:


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

谢谢@mudasobwa的回复,但请您详细说明一下答案,以便我能理解。还有我该怎么称呼它,或者我应该在哪里保存这些代码?更新。您不应该调用此代码,一旦运行,它将为您替换默认的
Rails
formatter,过滤掉所有垃圾。crap将被指定为
STOP\u WORDS\u REGEXP
# 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?
@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