5

I'm trying to disable logging of caching in production. Have succeeded in getting SQL to stop logging queries, but no luck with caching log entries. Example line in production log:

CACHE (0.0ms) SELECT `merchants`.* FROM `merchants` WHERE `merchants`.`id` = 1 LIMIT 1

I do not want to disable all logging, since I want logger.debug statements to show up in the production log. Using rails 3.2.1 with Mysql and Apache. Any suggestions?

1
  • 1
    I would like to know how to do the same thing using rails 2.3.5 Commented Aug 23, 2012 at 19:58

3 Answers 3

3

Rails 5.1

# config/initializers/active_record_logger.rb

class CacheFreeLogger < ActiveSupport::Logger
  def add(severity, message = nil, progname = nil, &block)
    return true if progname&.include? "CACHE"
    super
  end
end

ActiveRecord::Base.logger = CacheFreeLogger.new(STDOUT)
Sign up to request clarification or add additional context in comments.

Comments

2

I silenced the CACHE log using the method suggested by the post linked below.

I replaced the default ActiveRecod logger with a wrapper that filters off messages containing unwanted texts i.e. 'CACHE'.

First, create a file inside config/initializers for example active_record.rb and inside the file define a wrapper class and replace the active record logger like in the code below:

# Implementation of logger that ignores messages containing forbidden words
# here “CACHE” and "Settings Load"
class CacheFreeLogger < ActiveSupport::TaggedLogging

  @@excluded = ['Settings Load','CACHE']

  def add(severity, message = nil, progname = nil, &block)
    if message.nil?
      if block_given?
        message = block.call
      else
        message = progname
        progname = nil #No instance variable for this like Logger
      end
    end
    if severity > Logger::DEBUG ||  !(@@excluded.map{|e| message.include? e}.include?(true))
        @logger.add(severity, "#{tags_text}#{message}", progname)
    end
  end
end

#Replace the existing logger with the filtering one
ActiveRecord::Base.logger = CacheFreeLogger.new(ActiveRecord::Base.logger) if Rails.env.development?

The original post extended Logger not TaggedLoggin but it did not work for me.

This method was suggested in the blog: http://heliom.ca/blog/posts/disable-rails-cache-logging

Comments

-3

There are a few questions (this, this, this) like this on SO all with a similar theme. You could try putting this in an initializer file:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

Restore logger:

ActiveRecord::Base.logger = old_logger; nil # nil just to suppress some garbage 

1 Comment

How is your answer related to CACHE (0.0ms)-like entries?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.