Search code examples
loggingsinatrathinsinatra-assetpack

How do I turn off logging for asset requests with thin and sinatra?


I'm using sinatra/assetpack and thin. How do I turn off logging the asset requests like quiet assets for Rails?


Solution

  • Got assets turned off with this:

    module Sinatra
      module QuietLogger
    
        @extensions = %w(png gif jpg jpeg woff tff svg eot css js coffee scss)
    
        class << self
          attr_accessor :extensions
    
          def registered(app)
            ::Rack::CommonLogger.class_eval <<-PATCH
              alias call_and_log call
    
              def call(env)
                ext = env['REQUEST_PATH'].split('.').last
                if #{extensions.inspect}.include? ext
                  @app.call(env)
                else
                  call_and_log(env)
                end
              end
            PATCH
          end
        end
    
      end
    end
    

    And then simply register it in the app:

    configure :development
      register QuietLogger
    end