How can I disable logging of asset pipeline (sprockets) messages in Ruby on Rails 3.1?

Ruby on-RailsRuby on-Rails-3Asset PipelineSprocketsRails Sprockets

Ruby on-Rails Problem Overview


Sprockets tends to be quite verbose in the (dev) log by default under Ruby on Rails 3.1 (RC1):

Started GET "/assets/application.css" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/application.css.scss  (5ms)  (pid 6303)


Started GET "/assets/application.js" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/default.css.scss  (15ms)  (pid 6303)

...
Started GET "/assets/default/header_bg.gif" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Served asset /default/header_logo.gif - 304 Not Modified  (7ms)  (pid 6303)
Served asset /default/header_bg.gif - 304 Not Modified  (0ms)  (pid 6246)
Served asset /default/footer_bg.gif - 304 Not Modified  (49ms)  (pid 6236)
...

I'd like to either reduce the level of verbosity or disable it altogether.

I'm assuming there is a clean way to disable or reduce the verbosity of the logging by adding a config line in either environment.rb or development.rb similar to config.active_record.logger = nil which silences ActiveRecord SQL statements.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Place the following code in config/initializers/quiet_assets.rb

if Rails.env.development?
  Rails.application.assets.try(: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

Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes before_dispatch, and now we're going for the root rack call instead)

Update: A proper Rack middleware solution (instead of fragile alias_method_chain) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735

Solution 2 - Ruby on-Rails

Take a look at https://github.com/evrone/quiet_assets and just include it into your Gem file.

For the lazy: gem 'quiet_assets', group: :development

Solution 3 - Ruby on-Rails

For Ruby on Rails 3.2, add config.assets.logger = false to your development environment configuration file, typically found at config/environments/development.rb. See #4512.

Solution 4 - Ruby on-Rails

Two things are enough:

  1. config.assets.debug = false in config/enviroments/development.rb
  2. rake assets:precompile. See comment by @oma below; this is not needed

That's all!

Solution 5 - Ruby on-Rails

Eventually, it will be config.assets.logger = nil, but that part is currently stubbed on master (not done yet).

Solution 6 - Ruby on-Rails

I know it's an ugly and temporary solution, but I use this:

> tail -f log/development.log | grep -vE 'asset'

Solution 7 - Ruby on-Rails

Many people are confused about the use of config.assets.logger = false. Here is what it does and what it doesn't do.

According the source documentation:

> Setting config.assets.logger to false will turn off served assets logging.

However this probably is not what you think it is. It only disables sprocket 'serving' logs, not Ruby on Rails actionpack request logs. The Ruby on Rails maintainer explains this clearly here: https://github.com/rails/rails/issues/4569#issuecomment-3594500


Taking example from the link, logs like this are disabled:

> Served asset /jquery.isotope.js - 304 Not Modified (0ms)

But logs like this are not

> Started GET "/assets/jquery.isotope.js?body=1" for 127.0.0.1 at 2012-01-20 23:16:46 -0500

Solution 8 - Ruby on-Rails

config.assets.quiet = true

This is the latest way to go.

Solution 9 - Ruby on-Rails

In file development.rb in config/environments you'll find the line config.assets.debug = true.

Switch that to false and most of the asset load output will be gone. On my system only the two requests, for application.css and .js, remain.

Solution 10 - Ruby on-Rails

Use:

Rails.application.assets.logger = Logger.new(RUBY_PLATFORM =~ /(win|w)32$/ ? "NUL" : "/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'].index("/assets/") == 0
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
end

It's the same code choonkeat added. I just included it to work under Windows as well.

Solution 11 - Ruby on-Rails

In file config/environments/development.rb please add:

config.assets.debug = false

config.assets.logger = false

Solution 12 - Ruby on-Rails

Lograge for the win - it kills Ruby on Rails' annoying logger defaults out of the box (e.g. logging assets, logging partial rendering) and is customizable if you want to add/remove specific items.

Solution 13 - Ruby on-Rails

The previously mentioned linked solution helps:

https://github.com/evrone/quiet_assets

Also as below, it's working fine for me:

3.1 (only) (3.2 breaks before_dipatch)

app\config\initializers\quiet_assets.rb

Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
  def before_dispatch_with_quiet_assets(env)
    before_dispatch_without_quiet_assets(env) unless env['PATH_INFO'].index("/assets/") == 0
  end
  alias_method_chain :before_dispatch, :quiet_assets
end
3.2 Rails - Rack root tap approach
app\config\initializers\quiet_assets.rb

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'].index("/assets/") == 0
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
end

Solution 14 - Ruby on-Rails

In config/environments add config.log_level = :error to the .rb files you want to change. This will change the log settings to error only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionistvanpView Question on Stackoverflow
Solution 1 - Ruby on-RailschoonkeatView Answer on Stackoverflow
Solution 2 - Ruby on-RailsrouteView Answer on Stackoverflow
Solution 3 - Ruby on-RailsouranosView Answer on Stackoverflow
Solution 4 - Ruby on-RailsLisovsky VladView Answer on Stackoverflow
Solution 5 - Ruby on-RailscolinrossView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSucrenoirView Answer on Stackoverflow
Solution 7 - Ruby on-RailslulalalaView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAdam WaiteView Answer on Stackoverflow
Solution 9 - Ruby on-RailsTKABView Answer on Stackoverflow
Solution 10 - Ruby on-RailsCelso DantasView Answer on Stackoverflow
Solution 11 - Ruby on-RailsNajam TariqView Answer on Stackoverflow
Solution 12 - Ruby on-RailsYarinView Answer on Stackoverflow
Solution 13 - Ruby on-RailsSumit MunotView Answer on Stackoverflow
Solution 14 - Ruby on-RailsChris OView Answer on Stackoverflow