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?

Zielu
  • 8,312
  • 4
  • 28
  • 41
user1508459
  • 51
  • 1
  • 2

3 Answers3

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)
Watercycle
  • 497
  • 1
  • 7
  • 12
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

Zielu
  • 8,312
  • 4
  • 28
  • 41
-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 
Community
  • 1
  • 1
ScottJShea
  • 7,041
  • 11
  • 44
  • 67