2019-10-01 20:12:14 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
const uuid = require('uuid');
|
2019-10-01 20:12:14 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
module.exports = function initLogger ({ logger }) {
|
|
|
|
return function initLoggerMiddleware (req, res, next) {
|
|
|
|
const id = req.get('X-Request-Id') || uuid.v4();
|
|
|
|
res.locals.logger = logger.child({ id });
|
2019-10-01 20:12:14 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
res.locals.logger.info({ request: req });
|
|
|
|
res.on('finish', () => res.locals.logger.info({ response: res }));
|
|
|
|
res.on('close', () => res.locals.logger.info({ end: true }));
|
2019-10-01 20:12:14 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
next();
|
|
|
|
};
|
2019-10-01 20:12:14 +08:00
|
|
|
};
|