node.js - Express logging with storing the request information -
background
yes, there lot of different node.js logging library winston, bunyan , console.log. it's easy log down information of specific request when has called , when , information in response.
the problem
the problem begins sub function calls. when under 1 request calling multiple functions uses same logging, how pass request meta - data these log calls (function parameters seems 1 possible way these messy) ?
example
small visual coders:
// middleware set request based information app.use(function (req, res, next) { req.rid = 'random generated request id tracking sub queries'; }); app.get('/', function (req, rest) { async.series({ 'users': async.apply(db.users.find), 'posts': async.apply(db.posts.find), }, function (err, dbres) { console.log('api call made ', req.rid) res.end(dbres); }); }); // database functions in other file need track down request id in there (db.js) module.exports = { users: { find: function () { console.log('calling users listing ', req.rid); // error not possible access, not in scope // make query , return result } }, posts: { find: function () { console.log('calling post listing ', req.rid); // error not possible access, not in scope // make query , return result } } };
you can log requests simple conf in app.js with;
app.use(function(req, res, next){ console.log('%s %s', req.method, req.url); next(); });
however, need provide logs specific functions in controller.
Comments
Post a Comment