나는 sinatra/assetpack
과 thin
을 사용하고 있습니다. Rails에 대해 quiet assets과 같은 자산 요청 로깅을 끄려면 어떻게해야합니까?thin 및 sinatra를 사용하여 자산 요청에 대한 로깅을 어떻게 해제합니까?
2
A
답변
0
있어 자산이 함께 해제 :
다음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
그리고 단순히 응용 프로그램에 등록 :
configure :development
register QuietLogger
end
0
시나의 로거가 Rack::CommonLogger
단지 서브 클래스입니다. 더 중요한 것은, 로거 부분은 Sinatra App에 첨부 된 미들웨어입니다. 당신은과 같이시나에 추가 할 수있는 Rack::CommonLogger
모듈 (또는 당신의 자신의 미들웨어)를 기반으로 로거의 자신의 구현을 경우 :
use MyOwnLogger
우리는 Rack::CommonLogger
를 서브 클래스 및 로깅 장소 소요 보장하기 위해 call
방법을 수정합니다 신체가 데이터이고 자산이 아닐 경우에만.
module Sinatra # This need not be used. But namespacing is a good thing.
class SilentLogger < Rack::CommonLogger
def call(env)
status, header, body = @app.call(env)
began_at = Time.now
header = Rack::Utils::HeaderHash.new(header) # converts the headers into a neat Hash
# we will check if body is an asset and will log only if it is not an asset
unless is_asset?(body)
body = BodyProxy.new(body) { log(env, status, header, began_at) }
end
[status, header, body]
end
private
def is_asset?(body)
# If it just plain text, it won't respond to #path method
return false unless body.respond_to?(:path)
ext = Pathname.new(body.path).extname
ext =~ /\.png|\.jp(g|eg)|\.js/ ? true : false
end
end
end
그리고 앱에서 :
class App < Sinatra::Base
use Sinatra::SilentLogger
end
특히시나/assetpack''에서 그러한 기능입니다. Sinatra 앱에서 로깅을 완전히 끌 수는 있지만 원하는 것은 아닙니다. – Kashyap
@Kashyap 어떻게 로깅 호출을 가로 채고 자산 요청이 있는지 추측 할 수있는 방법은 무엇입니까? – AJcodez
Sinatra는 Rack :: CommonLogger를 로깅 용 인터페이스로 사용합니다. 각각의 응답에 대한 데이터를 기록하는'Rack :: CommonLogger'를 호출하는'Sinatra :: ComminLogger'라는 이름의 Base.rb 안에 클래스가 있습니다. – Kashyap