Logging ONLY SQL in your rails console

Say you’re interested in ONLY debugging some SQL.

But your rails console is spitting out GETs for assets.

Here’s one way to disable logging of the asset pipeline. Place the following code in config/initializers/quiet_assets.rb

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{^/assets/}

      call_without_quiet_assets(env)

    ensure

      Rails.logger.level = previous_level

    end

    alias_method_chain :call, :quiet_assets

  end

end

Also works with Rails 4 (although you need to create the file).

See also:

http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1?lq=1

and a possibly better way via a Rack middleware:

https://github.com/rails/rails/issues/2639#issuecomment-6591735

Note, in development.rb there’s a

config.assets.debug = true

which you can set to false. This reduces most of the 304’s for the asset pipeline but you still get the application css and js files being logged.

Obviously another way of doing this (and possibly the easiest) is to just do:

tail -f development.log | grep -v “asset”

Leave a Reply

Your email address will not be published. Required fields are marked *