Skip to content

Instantly share code, notes, and snippets.

@skwp
Created October 28, 2013 21:12
Show Gist options
  • Save skwp/7204787 to your computer and use it in GitHub Desktop.
Save skwp/7204787 to your computer and use it in GitHub Desktop.
A request/response logger Rack middleware.
module Middleware
class Logger
def initialize(app, logger)
@app = app
@logger = logger
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
@logger.info "Request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]} #{headers} #{request_params}"
@app.call(env).tap do |response|
status, headers, body = *response
@logger.info "Response: #{status}"
@logger.info "Headers: #{headers}"
@logger.info "Response:"
body.each do |line|
@logger.info line
end
end
end
end
end
@yarkm13
Copy link

yarkm13 commented Sep 28, 2016

How to use it?
I always get an error:
initialize': wrong number of arguments (1 for 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment