2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../../../shared/logging');
|
2016-10-25 14:17:43 +03:00
|
|
|
|
|
|
|
/**
|
2019-09-22 20:20:05 +03:00
|
|
|
* @TODO: move this middleware to ignition?
|
2016-10-25 14:17:43 +03:00
|
|
|
*/
|
|
|
|
module.exports = function logRequest(req, res, next) {
|
2019-09-22 20:20:05 +03:00
|
|
|
const startTime = Date.now();
|
2016-10-25 14:17:43 +03:00
|
|
|
|
|
|
|
function logResponse() {
|
|
|
|
res.responseTime = (Date.now() - startTime) + 'ms';
|
|
|
|
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) {
|
2020-04-09 21:40:00 +03:00
|
|
|
logging.error({req: req, res: res, err: req.err});
|
2016-10-25 14:17:43 +03:00
|
|
|
} else {
|
2020-04-09 21:40:00 +03:00
|
|
|
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();
|
|
|
|
};
|