Class: Rack::LogRequest
- Inherits:
-
Object
- Object
- Rack::LogRequest
- Defined in:
- lib/rack-log-request.rb,
lib/rack-log-request/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ LogRequest
constructor
Intialize the LogRequest Middleware.
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, = {}) @app = app unless .has_key?(:mongo_db) @mongo_connection = Mongo::Connection.new([:host], [:port], :timeout => 5) if .has_key?(:user) @mongo_connection.add_auth([:database], [:user], [:password]) end @mongo_database = @mongo_connection.db([:database]) else @mongo_database = [: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 |