title: Node request/connection-specific variables
body: <p><strong>Context:</strong></p>
  
  <p>I'm trying to link log entries to the request associated with the generation of those entries. I like the concept of bunyan's <code>child</code> logger, which allows me to attach a request id to any later entries made by the child logger. I wanted this logger to be used in other modules, so I <code>require</code> a file with a getter to get the child logger. This allows an otherwise isolated event such as <code>getting widgets</code> to be linked to the request that caused that entry</p>
  
  <p>Contrived example:</p>
  
  <p><code>index.js</code></p>
  
  <pre><code>var widget = require('./widget');
  var logging = require('./logging');
  //...
  app.use('*', function(req, res, next) {
      var logger = logging.getLogger();
      var child = logger.child({requestId: req.id}); //req.id set in earlier middleware
      logging.setLogger(child);
  };
  app.get('/widgets', function(req, res, next) { 
      res.json(widgets.getAll());
  };
  //...
  </code></pre>
  
  <p><code>widget.js</code></p>
  
  <pre><code>var logging = require('./logging');
  //...
  exports = module.exports = {
      getAll: function () {
          logging.getLogger().info("getting widgets");
          return widgets;
      }
  }
  </code></pre>
  
  <p>The problem with this is that the state is shared between requests (singletons?), so if two requests are made nearly simultaneously, the "getting widgets" log entry will have the <code>requestId</code> property set to the second request's id.</p>
  
  <p><strong>The question:</strong></p>
  
  <p>Is there a way to have variables whose scope is attached to the request, without having to inject and pass around the <code>req</code> object (e.g. <code>new widget()</code> and/or <code>widget.setRequest(req)</code>), or instantiating as injecting the <code>logging</code> object? Passing objects like this into other modules feels like violating separation of concerns</p>
  
  <p>Put another way, what is the best way to share state for a request, rather than across requests, to implement what I'm trying to achieve?</p>