Class: Rack::LogRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-log-request.rb,
lib/rack-log-request/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

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

Intialize the LogRequest Middleware.

@params app the Rack application @params options all options about this middleware @opts mongo_db a Mongo::Database object @opts host the host of your MongoDB database @opts port the port of your MongoDB database @opts database the database of your MongoDB database @opts user the user to access your MongoDB database @opts password the password of your MongoDB user to access database



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack-log-request.rb', line 17

def initialize(app, options = {})
  @app = app
  unless options.has_key?(:mongo_db)
    @mongo_connection =  Mongo::Connection.new(options[:host], options[:port], :timeout => 5)
    if options.has_key?(:user)
      @mongo_connection.add_auth(options[:database], options[:user], options[:password])
    end
    @mongo_database = @mongo_connection.db(options[:database])
  else
    @mongo_database = options[:mongo_db]
  end
  @mongo_collection = @mongo_database.create_collection('rack-log-request', {:capped => true, :size => 100, :max => 10})
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rack-log-request.rb', line 31

def call(env)
  t = Time.now
  status, headers, response = @app.call(env)
  @mongo_collection.insert({:request => env['REQUEST_URI'],
                            :status => status,
                            :size => headers['Content-Length'],
                            :millis => (Time.now - t)*1000})
  return [status, headers, response]
end