Class: Rack::HTTPLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/http-logger.rb

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ HTTPLogger

Returns a new instance of HTTPLogger.



5
6
7
8
9
10
11
12
13
14
# File 'lib/rack/http-logger.rb', line 5

def initialize(app, options = {})
  @app = app

  @stream = options[:stream] || $stdout
  @stream.sync = true unless options.fetch(:sync, true)
  @source = options[:source] || "rack-http-logger"

  @method = options[:method] ? "#{options[:method]}".upcase : "LOG"
  @path = options[:path] || "/"
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/http-logger.rb', line 16

def call(env)
  request = Rack::Request.new(env)

  return @app.call(env) unless request.request_method == @method and request.path == @path

  if request.media_type == "application/json" and (body = request.body.read).length.nonzero?
    log JSON.parse(body)
  else
    log request.params
  end

  [201, {"Content-Type" => "text/plain"}, []]
end