Ghost/core/server/web/middleware/log-request.js
kirrg001 6f6c8f4521 Import lib/common only
refs #9178

- avoid importing 4 modules (logging, errors, events and i18n)
- simply require common in each file
2017-12-12 10:28:13 +01:00

31 lines
871 B
JavaScript

var uuid = require('uuid'),
common = require('../../lib/common');
/**
* @TODO:
* - move middleware to ignition?
*/
module.exports = function logRequest(req, res, next) {
var startTime = Date.now(),
requestId = req.get('X-Request-ID') || uuid.v1();
function logResponse() {
res.responseTime = (Date.now() - startTime) + 'ms';
req.requestId = requestId;
req.userId = req.user ? (req.user.id ? req.user.id : req.user) : null;
if (req.err && req.err.statusCode !== 404) {
common.logging.error({req: req, res: res, err: req.err});
} else {
common.logging.info({req: req, res: res});
}
res.removeListener('finish', logResponse);
res.removeListener('close', logResponse);
}
res.on('finish', logResponse);
res.on('close', logResponse);
next();
};