Ghost/core/server/middleware/log-request.js
Katharina Irrgang 8c54f3cca9 Added request id header (#9122)
no issue

- be able to track a request through different systems
- case insensitive
- manual test only
2017-10-09 14:48:32 +02:00

31 lines
852 B
JavaScript

var uuid = require('uuid'),
logging = require('../logging');
/**
* @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) {
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();
};