Skip to content

Instantly share code, notes, and snippets.

@nisanthchunduru
Created June 26, 2023 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisanthchunduru/e82fa3674ca36f8d18ec1354e1f02726 to your computer and use it in GitHub Desktop.
Save nisanthchunduru/e82fa3674ca36f8d18ec1354e1f02726 to your computer and use it in GitHub Desktop.
Log responses in Rails in development environment
# In config/application.rb
class ResponseLogger
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
puts "Request: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]} #{headers} #{request_params}"
@app.call(env).tap do |response|
status, headers, body = *response
puts "Response Status: #{status}"
puts "Response Headers: #{headers}"
puts "Response Body: #{body.join("\n)}"
end
end
end
module Shyftplan
class Application < Rails::Application
config.middleware.use(ResponseLogger);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment