lib/falcon/verbose.rb in falcon-0.12.0 vs lib/falcon/verbose.rb in falcon-0.13.0

- old
+ new

@@ -17,47 +17,41 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'async/logger' +require 'async/http/statistics' module Falcon - class Verbose + class Verbose < Async::HTTP::Middleware def initialize(app, logger = Async.logger) - @app = app + super(app) + @logger = logger end - def annotate(env, task = Async::Task.current) - request_method = env['REQUEST_METHOD'] - request_path = env['PATH_INFO'] - remote_address = env['REMOTE_ADDR'] + def annotate(request, peer: nil, address: nil) + task = Async::Task.current - task.annotate("#{request_method} #{request_path} for #{remote_address}") + # @logger.debug "#{request.method} #{request.path} #{request.version} from #{address.inspect}" + # @logger.debug request.headers.inspect + + task.annotate("#{request.method} #{request.path} from #{address.inspect}") end - def log(start_time, env, response, error) - duration = Time.now - start_time + def call(request, **options) + annotate(request, **options) - request_method = env['REQUEST_METHOD'] - request_path = env['PATH_INFO'] - server_protocol = env['SERVER_PROTOCOL'] + statistics = Async::HTTP::Statistics.start - if response - status, headers, body = response - @logger.info "#{request_method} #{request_path} #{server_protocol} -> #{status}; Content length #{headers.fetch('Content-Length', '-')} bytes; took #{duration} seconds" - else - @logger.info "#{request_method} #{request_path} #{server_protocol} -> #{error}; took #{duration} seconds" + response = super + + statistics.wrap(response) do |statistics, error| + @logger.info "#{request.method} #{request.path} #{request.version} -> #{response.status}; #{statistics.inspect}" + # @logger.info response.headers.inspect + @logger.error "#{error.class}: #{error.message}" if error end - end - - def call(env) - start_time = Time.now - annotate(env) - - response = @app.call(env) - ensure - log(start_time, env, response, $!) + return response end end end