return to list of articles

Silencing Rails 5 asset log errors

How to silence log errors for missing assets in development in rails 5 if you don't use sprockets

Quiet assets

Once upon a time a cool little gem called quiet_assets was useful for turning off the Rails asset pipeline logging in development. But as of sprockets-rails 3.1.0 the gem has been deprecated. Rails now ships with a way for you to silence asset logging right out of the box.

All you have to do now is open up config/environments/development.rb and configure assets to be quiet:

Rails.application.configure do
  config.assets.quiet = true
end

But I don’t use the asset pipeline

If you’re like me and you’ve stripped out the asset pipeline in favor of a webpack approach, the above approach won’t work because Rails doesn’t know what config.assets is. Have no fear. All you need to do is create an initializer file like config/initializers/quiet_assets.rb and add the following:

if Rails.env.development?
  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/}
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{/favicons/}
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{/javascripts/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

Add any additional PATH_INFO regex you may need and your logs will begin looking sane again. Good luck!


Get notified when Pawel releases new posts, guides, or projects