Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time at which a request is made from the request object in rails 3?

I need to log the time at which a request is made. How can I get that info from a request object in rails 3?

like image 249
Sathish Avatar asked Apr 14 '11 09:04

Sathish


2 Answers

You need to create your Rack middleware, which will add a timestamp to request. For instance (haven't checked it, but it gives you the idea):


module Middleware
  class Timestamp
    def initialize(app)
      @app = app
    end

    def call(env)
      env[:timestamp] = Time.now
      @app.call(env)
    end
  end
end

And add it in application.rb (config.middleware.use)

like image 95
Roman Avatar answered Sep 22 '22 12:09

Roman


The request.env does not seem to include a timestamp:

<% for item in request.env %>
  <%= item %><br />
<% end %>

And I don't see anything related here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html

Is it sufficient for your application to just set a timestamp in your controller in a before_filter?

like image 40
Markus Proske Avatar answered Sep 20 '22 12:09

Markus Proske