Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Rails Cache log level separately?

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?

like image 547
kyrre Avatar asked Aug 31 '11 08:08

kyrre


People also ask

How can you implement caching in Rails?

You can create your own custom cache store by simply extending ActiveSupport::Cache::Store and implementing the appropriate methods. This way, you can swap in any number of caching technologies into your Rails application. To use a custom cache store, simply set the cache store to a new instance of your custom class.

What is low level caching?

What is Low-Level Caching. What Rails calls low level caching is really just reading and writing data to a key-value store. Out of the box, Rails supports an in-memory store, files on the filesystem, and external stores like Redis or memcached. It is called "low level" caching because you are dealing with the Rails.

Does Rails cache use Redis?

Rails 5.2 introduced built-in Redis cache store, which allows you to store cache entries in Redis.

Where does Rails store cache?

Rails (as of 2.1) provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk. Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings.


1 Answers

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/#{ENV['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!
like image 186
dexter Avatar answered Oct 27 '22 06:10

dexter