How to set Rails Cache log level separately?

2019-03-29 09:30发布

I use a pretty old rails version which is 2.3.2 because of legacy project.

I set global log_level to :debug in our rails app. But since we also use Rails.cache the log file are full of annoying lines such as
Cache read: ...
Cache miss: ...

I want to just suppress these but not affect other 'more useful' info such as SQL logging.

How to do that?

1条回答
戒情不戒烟
2楼-- · 2019-03-29 10:01

Well, after initializing your cache store (in the example below, I use memory store) in your specific environment.rb file, you can redirect cache_store's log to a separate file and also tweak the logger level:

config.cache_store = ActiveSupport::Cache::MemoryStore.new(:expires_in => 5.minutes)
config.cache_store.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_cache.log")
config.cache_store.logger.level = Logger::INFO

In addition to that, the cache store has a method called silence! that will turn off the logger :-|

config.cache_store.silence!
查看更多
登录 后发表回答