How to generate a unique request ID in Rails?

后端 未结 3 509
一个人的身影
一个人的身影 2021-01-11 10:16

I need a unique request ID for my logger, so I can track each request in the log file.

So far I got this

REQUEST_ID = Digest::MD5.hexdigest(Time.now.         


        
3条回答
  •  一整个雨季
    2021-01-11 10:56

    Follow-up to the answer using log_tags, since :uuid is only unique among a request, it is difficult to use it with session tracking.

    I found log_tags accept a Proc, and pass a request object as an parameter. So, following code will stamp all of log entry with session_id (assuming you're using ActiveRecord based session store)

    config.log_tags = [ lambda {|req| "#{req.cookie_jar["_session_id"]}" }, :remote_ip, :uuid  ]
    

提交回复
热议问题