gpt4 book ai didi

ruby-on-rails - 如何禁用 rails 中片段缓存的日志

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:43 25 4
gpt4 key购买 nike

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

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

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

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?

仍然没有帮助。

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

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

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

关于ruby-on-rails - 如何禁用 rails 中片段缓存的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com