如何在Rails中生成唯一的请求ID? [英] How to generate a unique request ID in Rails?

查看:276
本文介绍了如何在Rails中生成唯一的请求ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我得到了这个

  REQUEST_ID = Digest :: MD5.hexdigest(Time.now.to_f_sto_s + $ PID.to_s)

问题是我不知道该把它放在哪里。我尝试将它放在我的自定义记录器文件中,在课堂之外。但它必须被缓存或什么东西,因为我总是得到相同的散列。



有什么想法?



笔记。我使用Rails 3和Passenger standalone

更新:

Rails 3.2 :uuid标签不起作用。看看日志格式有多糟糕:

  [0909413851b79676cb06e0842d21c466] [127.0.0.1] 

开始HEAD /for 127.0.0.1 at 2月21日14:08:25 -0300 2012
[0909413851b79676cb06e0842d21c466] [127.0.0.1]以PagesController#home为处理HTML
[0909413851b79676cb06e0842d21c466] [127.0.0.1] bla
[0909413851b79676cb06e0842d21c466] [127.0.0.1]布局/应用程序中的渲染页面/ home.html.erb(2.0ms)

在制作过程中,这将是一团糟。注意第一行之后的换行符?现在想象一下,如何在每秒处理很多请求的服务器中看到日志。将请求与URI关联很难

解决方案

如果要在请求日志行中插入请求UUID 在2月21日星期二14:00:00 -0300 2012 开始获得127.0.0.1 GET/,您可以修补或子类 Rails :: Rack :: Logger 修改 call_app 方法:

  def call_app(env)
request = ActionDispatch :: Request.new(env)
path = request.filtered_pa​​th
Rails.logger.info\\\
\\\
Started#{request。 request_method} \#{path} \为#{Time.now.to_default_s}上的#{request.ip}
@ app.call(env)
确保
ActiveSupport :: LogSubscriber.flush_all!
end

请求对象是在日志语句之前创建的,因此您可以更改记录声明包含 request.uuid


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.to_f.to_s + $PID.to_s)

The problem is that I don't know where to put this. I tried placing it inside my custom logger file, outside the class. But it must be being cached or something because I'm always getting the same hash.

Any ideas?

note. I'm using Rails 3 and Passenger standalone

UPDATE:

Rails 3.2 :uuid tag won't work. Look how badly formatted the logs are:

[0909413851b79676cb06e0842d21c466] [127.0.0.1] 

Started HEAD "/" for 127.0.0.1 at Tue Feb 21 14:08:25 -0300 2012
[0909413851b79676cb06e0842d21c466] [127.0.0.1] Processing by PagesController#home as HTML
[0909413851b79676cb06e0842d21c466] [127.0.0.1] bla
[0909413851b79676cb06e0842d21c466] [127.0.0.1]   Rendered pages/home.html.erb within layouts/application (2.0ms)

In production this will be a mess. Notice the newlines after the first line? Now imagine how the logs would look like in a server handling many requests per second. It will be hard to associate a request with a URI

解决方案

If you want to insert the request UUID at the log line Started GET "/" for 127.0.0.1 at Tue Feb 21 14:00:00 -0300 2012, you can patch or subclass Rails::Rack::Logger to modify the call_app method:

def call_app(env)
  request = ActionDispatch::Request.new(env)
  path = request.filtered_path
  Rails.logger.info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
  @app.call(env)
ensure
  ActiveSupport::LogSubscriber.flush_all!
end

The request object is created right before the logging statement, so you can change the logging statement to include request.uuid.

这篇关于如何在Rails中生成唯一的请求ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆