title: How to generate a unique request ID in Rails?
body: <p>I need a unique request ID for my logger, so I can track each request in the log file.</p>
  
  <p>So far I got this</p>
  
  <pre><code>REQUEST_ID = Digest::MD5.hexdigest(Time.now.to_f.to_s + $PID.to_s)
  </code></pre>
  
  <p>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.</p>
  
  <p>Any ideas?</p>
  
  <p>note. I'm using Rails 3 and Passenger standalone</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Rails 3.2 :uuid tag won't work. Look how badly formatted the logs are:</p>
  
  <pre><code>[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)
  </code></pre>
  
  <p>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</p>