Ghost/core/server/web/parent/middleware/log-request.js
Vikas Potluri 00c324fa4e
Moved core/server/lib/common/logging to core/shared/logging (#11857)
- Represents that logging is shared across all parts of Ghost at present
  * moved core/server/lib/common/logging to core/shared/logging
  * updated logging path for generic imports
  * updated migration and schema imports of logging
  * updated tests and index logging import
  * 🔥 removed logging from common module
  * fixed tests
2020-05-28 19:30:23 +01:00

27 lines
751 B
JavaScript

const logging = require('../../../../shared/logging');
/**
* @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();
};