-
-
Save quinn/a7b020620a15c9a31b722c110da6ad38 to your computer and use it in GitHub Desktop.
A request/response logger Rack middleware.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Middleware | |
class Logger | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
headers = env.select {|k,v| k.start_with? 'HTTP_'} | |
.map {|pair| [pair[0].sub(/^HTTP_/, ''), pair[1]].join(": ")} | |
.sort | |
request_params = env['rack.input'].read | |
Rails.logger "Request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]} #{headers} #{request_params}" | |
@app.call(env).tap do |response| | |
status, headers, body = *response | |
Rails.logger "Response: #{status}" | |
Rails.logger "Headers: #{headers}" | |
Rails.logger "Response:" | |
# body.each do |line| | |
# @logger.info line | |
# end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment