mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 14:22:07 +03:00
e53baac13d
- Moved log-request from shared to parent as it is not shared - This file is only used in one place, this updates the code structure to reflect this - This is one of many similar changes needed to make it easier to refactor to the existing setup
27 lines
746 B
JavaScript
27 lines
746 B
JavaScript
const {logging} = require('../../../lib/common');
|
|
|
|
/**
|
|
* @TODO: move this middleware to ignition?
|
|
*/
|
|
module.exports = function logRequest(req, res, next) {
|
|
const startTime = Date.now();
|
|
|
|
function logResponse() {
|
|
res.responseTime = (Date.now() - startTime) + 'ms';
|
|
req.userId = req.user ? (req.user.id ? req.user.id : req.user) : null;
|
|
|
|
if (req.err && req.err.statusCode !== 404) {
|
|
logging.error({req: req, res: res, err: req.err});
|
|
} else {
|
|
logging.info({req: req, res: res});
|
|
}
|
|
|
|
res.removeListener('finish', logResponse);
|
|
res.removeListener('close', logResponse);
|
|
}
|
|
|
|
res.on('finish', logResponse);
|
|
res.on('close', logResponse);
|
|
next();
|
|
};
|