Skip to content

Commit

Permalink
config.assets.quiet logger silencing with middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcphillips committed Jun 9, 2016
1 parent b4f9006 commit 91da721
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Add additional assets to compile on deploy. Defaults to `application.js`, `appli

Add additional load paths to this Array. Rails includes `app/assets`, `lib/assets` and `vendor/assets` for you already. Plugins might want to add their custom paths to this.

**`config.assets.quiet`**

Suppresses logger output for asset requests. Uses the `config.assets.prefix` path to match and wraps the request in a `Rails.logger.silence{ }` which silences all but `ERROR` level. Defaults to `false`.

**`config.assets.version`**

Expand Down
18 changes: 18 additions & 0 deletions lib/sprockets/rails/quiet_assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Sprockets
module Rails
class QuietAssets
def initialize(app)
@app = app
@assets_regex = %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
end

def call(env)
if env['PATH_INFO'] =~ @assets_regex
::Rails.logger.silence { @app.call(env) }
else
@app.call(env)
end
end
end
end
end
8 changes: 8 additions & 0 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'sprockets'
require 'sprockets/rails/context'
require 'sprockets/rails/helper'
require 'sprockets/rails/quiet_assets'
require 'sprockets/rails/route_wrapper'
require 'sprockets/rails/version'
require 'set'
Expand Down Expand Up @@ -97,6 +98,7 @@ def configure(&block)
config.assets.precompile = []
config.assets.prefix = "/assets"
config.assets.manifest = nil
config.assets.quiet = false

initializer :set_default_precompile do |app|
if using_sprockets4?
Expand All @@ -107,6 +109,12 @@ def configure(&block)
end
end

initializer :quiet_assets do |app|
if app.config.assets.quiet
app.middleware.insert_before ::Rails::Rack::Logger, ::Sprockets::Rails::QuietAssets
end
end

config.assets.version = ""
config.assets.debug = false
config.assets.compile = true
Expand Down
41 changes: 41 additions & 0 deletions test/test_quiet_assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'active_support'
require 'active_support/testing/isolation'
require 'active_support/log_subscriber/test_helper'
require 'minitest/autorun'

require 'sprockets/railtie'
require 'rails'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class TestQuietAssets < Minitest::Test
include ActiveSupport::Testing::Isolation

def setup
@app = Class.new(Rails::Application)
@app.config.eager_load = false
@app.config.logger = ActiveSupport::Logger.new("/dev/null")
@app.initialize!

Rails.logger.level = Logger::DEBUG
end

def test_silences_with_default_prefix
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/assets/stylesheets/application.css")
end

def test_silencess_with_custom_prefix
Rails.application.config.assets.prefix = "path/to"
assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/path/to/thing")
end

def test_does_not_silence_without_match
assert_equal Logger::DEBUG, middleware.call("PATH_INFO" => "/path/to/thing")
end

private

def middleware
@middleware ||= Sprockets::Rails::QuietAssets.new(->(env) { Rails.logger.level })
end
end
21 changes: 21 additions & 0 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,25 @@ def test_direct_build_environment_call
assert_equal ["#{ROOT}/app/assets/config", "#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
env.paths.sort
end

def test_quiet_assets_defaults_to_off
app.initialize!
app.load_tasks

assert_equal false, app.config.assets.quiet
refute app.config.middleware.include?(Sprockets::Rails::QuietAssets)
end

def test_quiet_assets_inserts_middleware
app.configure do
config.assets.quiet = true
end
app.initialize!
app.load_tasks
middleware = app.config.middleware

assert_equal true, app.config.assets.quiet
assert middleware.include?(Sprockets::Rails::QuietAssets)
assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
end
end

0 comments on commit 91da721

Please sign in to comment.