Class: Imgur::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger) ⇒ Logger

Returns a new instance of Logger.



3
4
5
# File 'lib/imgur/logger.rb', line 3

def initialize(app, logger)
  @app, @logger = app, logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



2
3
4
# File 'lib/imgur/logger.rb', line 2

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/imgur/logger.rb', line 6

def call(env)
  request = Rack::Request.new(env)
  headers = env.keys.select{|k| k.match(/HTTP_/)}
  logger.debug { "--> #{request.request_method} #{request.url} #{request.params}\n#{headers.inject([]){|r,k| r + ["#{k.gsub("HTTP_", "")}: #{env[k].inspect}"]}.join("\n")}\n#{request.body.tap(&:rewind).read}" }
  request.body.tap(&:rewind)
  status, headers, body = @app.call(env)
  real_body = ""
  body.each{|b| real_body += b.to_s}
  logger.debug { "<-- #{status} #{request.url}\n#{headers.inject([]){|r,(k,v)| r + ["#{k.upcase}: #{v}"]}.join("\n")}\n#{real_body}" }
  [status, headers, body]
rescue => e
  logger.error { "<-- #{e.inspect}" }
  raise
end