Created
December 6, 2021 09:18
-
-
Save ktimothy/ac7c7b14d10f4c879d8acf1d8c85965f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class LogRequestAndResponse | |
def initialize(app); @app = app; end | |
def call(env) | |
req = Rack::Request.new(env) | |
headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'} | |
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]} | |
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]} | |
.sort | |
.flatten] | |
puts "<-- #{req.request_method} #{req.url} #{req.params.inspect} #{headers.inspect}" | |
status, headers, body = @app.call(env) | |
puts "--> #{status} #{(JSON.load(body.first) rescue '- stripped -').inspect} #{headers.inspect}" | |
[status, headers, body] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment