'Rails log too verbose

How can I prevent Rails to log too much? Here is a typical trace in my production.log file, many partials, cache hits... It's useful in development but I don't want it in my production environment.

Started GET "/?redirected=true" for 46.193.131.53 at 2012-08-16 18:39:20 +0200
Processing by HomeController#index as HTML
  Parameters: {"redirected"=>"true"}
  Rendered application/_successfully_connected.html.haml (0.8ms)
  Rendered hotspot_infos/_infos.html.haml (0.4ms)
  Rendered application/_hotspot_infos.html.haml (1.8ms)
  Rendered application/_news.html.haml (0.3ms)
Read fragment views/social-zone-341-directory (0.5ms)
  Rendered application/_directory.html.haml (2.5ms)
  Rendered application/_meteo.html.haml (1.1ms)
  Rendered application/_notifications.html.haml (0.8ms)
  Rendered application/_like_button.html.haml (0.3ms)
  Rendered application/_navbar.html.haml (4.2ms)
  Rendered application/_connection.html.haml (0.5ms)
  Rendered application/_gallery.html.haml (0.2ms)
  Rendered application/_search_bar.html.haml (0.4ms)
  Rendered pictures/_picture_frame.html.haml (0.3ms)
  Rendered application/_profile_preview.html.haml (1.4ms)
  Rendered application/_profile_block.html.haml (1.7ms)
  Rendered application/_menus.html.haml (3.3ms)
  Rendered application/_left_pane.html.haml (5.5ms)
  Rendered application/_langs.html.haml (0.8ms)
  Rendered application/_footer.html.haml (1.9ms)
  Rendered application/_flash_modal.html.haml (0.1ms)
  Rendered application/_connection_required.js.erb (0.2ms)
Completed 200 OK in 159ms (Views: 25.5ms | ActiveRecord: 88.0ms)

Thank's for your help

PS: I'm using Rails 3.2.6



Solution 1:[1]

In Rails 4 there will be a setting to clean up logs:

config.action_view.logger = nil

To achieve that in Rails 3, you have to monkey patch ActionView:

module ActionView
  class LogSubscriber < ActiveSupport::LogSubscriber
    def logger
      @memoized_logger ||= Logger.new('/dev/null')
    end
  end
end

Solution 2:[2]

You need to set your config.log_level differently. Learn about Log Levels.

For example, add the following to config/evironments/production.rb

config.log_level = :warn

Yours is likely set to :debug or :info.

Solution 3:[3]

I put this in my initializers to monkeypatch certain logging to go to debug instead of info:

module ActionView
  class LogSubscriber
    def render_template(event)
      message = "Rendered #{from_rails_root(event.payload[:identifier])}"
      message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
      message << " (#{event.duration.round(1)}ms)"
      debug(message)
    end
    alias :render_partial :render_template
    alias :render_collection :render_template
  end  
end

module ActionController
  class LogSubscriber
    # Use debug logging for read_fragment
    # %w(write_fragment read_fragment exist_fragment? expire_fragment expire_page write_page).each do |method|
    %w(read_fragment).each do |method|
      class_eval <<-METHOD, __FILE__, __LINE__ + 1
        def #{method}(event)
          return unless logger.info?
          key_or_path = event.payload[:key] || event.payload[:path]
          human_name  = #{method.to_s.humanize.inspect}
          debug("\#{human_name} \#{key_or_path} (\#{event.duration.round(1)}ms)")
        end
      METHOD
    end
  end
end

Solution 4:[4]

Here in 2021, I suggest using the semantic_logger (and rails_semantic_logger) gem(s) for this. (Docs link)

Lograge is also an option, but it seems less well-maintained at this point.

Solution 5:[5]

Log levels can be set on a per-component basis. It's possible, for example, to set the general log_level to INFO, but set the components to a quieter level.

# config/environments/production.rb
config.log_level = :info

# Build quieter loggers for particular components.
build_logger = ->(level) {
  # This is the recommend way to build a logger, from
  # https://guides.rubyonrails.org/v6.1/configuring.html
  logger = ActiveSupport::Logger.new(STDOUT, level: level)
  logger.formatter = config.log_formatter
  ActiveSupport::TaggedLogging.new(logger)
}
config.action_controller.logger = build_logger[::Logger::ERROR]
config.active_job.logger = build_logger[::Logger::ERROR]
config.action_view.logger = build_logger[::Logger::ERROR]
config.assets.logger = build_logger[::Logger::ERROR]
config.action_mailer.logger = build_logger[::Logger::ERROR]

This is a configuration that my team is considering for a production app with a throughput of 2-3 kilorequests per minute. It will allow us to write low-volume INFO-level logging for important business logic, without overwhelming our logging budget with high-volume INFO-level logging from components like ActionController.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 deefour
Solution 3 Joris
Solution 4 antgel
Solution 5 Jared Beck