2017-01-04 19:10:29 +03:00
|
|
|
var uuid = require('uuid'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common');
|
2016-10-25 14:17:43 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @TODO:
|
|
|
|
* - move middleware to ignition?
|
|
|
|
*/
|
|
|
|
module.exports = function logRequest(req, res, next) {
|
|
|
|
var startTime = Date.now(),
|
2017-10-09 15:48:32 +03:00
|
|
|
requestId = req.get('X-Request-ID') || uuid.v1();
|
2016-10-25 14:17:43 +03:00
|
|
|
|
|
|
|
function logResponse() {
|
|
|
|
res.responseTime = (Date.now() - startTime) + 'ms';
|
|
|
|
req.requestId = requestId;
|
|
|
|
req.userId = req.user ? (req.user.id ? req.user.id : req.user) : null;
|
|
|
|
|
2017-08-08 13:48:10 +03:00
|
|
|
if (req.err && req.err.statusCode !== 404) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error({req: req, res: res, err: req.err});
|
2016-10-25 14:17:43 +03:00
|
|
|
} else {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.info({req: req, res: res});
|
2016-10-25 14:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
res.removeListener('finish', logResponse);
|
|
|
|
res.removeListener('close', logResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.on('finish', logResponse);
|
|
|
|
res.on('close', logResponse);
|
|
|
|
next();
|
|
|
|
};
|