4

I have an ajax call occurring every n seconds to check queue status and modify a widget's progress bar on my page. This, of course, dumps the call into the log. I would like to filter the action's entry into the log. Can this be done with Logger?

Example

class QueueCallersController < ApplicationController

   # code to stop logger from logging my_ajax_action?       

   def my_action

     # auto log this

   end

   def my_ajax_action

     #  do not log this

   end

end

3 Answers3

0

You can use a custom logger as described here:

http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/

Basically you swap out the default logger with a custom logger on a per-action basis which silences all output for that action. You will need to specify the paths to be silenced somewhere else. This has the advantage of being cleaner and less invasive than adding silencing code within the action itself.

Josh Rieken
  • 2,256
  • 1
  • 19
  • 23
0

The working solution is based off of the code found at https://github.com/rails/rails/issues/2639#issuecomment-6591735 and was found, via an update link, through Ahmad Sherif's answer.

# Usage: in develoopment.rb
#
#   config.middleware.insert_before Rails::Rack::Logger, DisableAssetsLogger
#
class DisableAssetsLogger
  def initialize(app)
    @app = app
    Rails.application.assets.logger = Logger.new('/dev/null')
  end

  def call(env)
    previous_level = Rails.logger.level
    Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
    @app.call(env)
  ensure
    Rails.logger.level = previous_level
  end
end
Community
  • 1
  • 1
-1

Well, I'm sure there are many ways to approach this problem, but here's one based on this answer about silencing assets log.

if Rails.env.development?
  Rails.application.assets.logger = Logger.new('/dev/null')
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/queue_callers/my_ajax_action/} # Change the path here
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

Add the code to a file in config/initializers directory then restart the server.

Community
  • 1
  • 1
Ahmad Sherif
  • 5,923
  • 3
  • 21
  • 27