Class: ApiGW::Logger

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/apigw/logger.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apigw/logger.rb', line 19

def call(env)
  lines = [ "" ]
  if env.url.query.nil?
    path = env.url.path
  else
    path = "#{env.url.path}?#{env.url.query}"
  end
  lines << "#{env.method.upcase} #{path} HTTP/1.1"
  env.request_headers.keys.sort.each do |key|
    lines << "#{key}: #{env.request_headers[key]}"
  end
  lines << pretty_json(env.body)
  lines << ""

  @app.call(env).on_complete do
    lines << "HTTP/#{env[:http_version]} #{env.status} #{env[:message]}"
    env.response_headers.keys.sort.each do |key|
      lines << "#{key}: #{env.response_headers[key]}"
    end
    lines << pretty_json(env.body)
    puts lines.join "\n"
  end

end