An interesting way to do this would be the new Domains feature. http://nodejs.org/api/domain.html
Domains, providing excellent error recovery, can be used as a type of "Local stream storage" - basically, storing data for each request.
Create some middleware that adds each request / response to the domain.
app.use(function(req, res, next) { var reqd = domain.create(); reqd.add(req); reqd.add(res); reqd._req = req;
In the log function, you can now get the current domain and pull out the request object.
function log_message(level, message) {
Domains are still experimental, but it doesn't seem like much will change between now and release 1.0.
The reddest
source share