开发者

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

开发者 https://www.devze.com 2023-02-25 17:00 出处:网络
I need to log the time at which a request is made. How can I get that info from a request 开发者_C百科object in rails 3?You need to create your Rack middleware, which will add a timestamp to request.

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


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)


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消