2018-09-20 16:03:33 +03:00
|
|
|
const uuid = require('uuid');
|
2018-09-20 21:04:34 +03:00
|
|
|
const common = require('../../../lib/common');
|
2016-10-25 14:17:43 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @TODO:
|
|
|
|
* - move middleware to ignition?
|
|
|
|
*/
|
|
|
|
module.exports = function logRequest(req, res, next) {
|
2018-09-10 15:07:57 +03:00
|
|
|
const 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();
|
|
|
|
};
|