2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-02 01:18:15 +08:00
|
|
|
const uuid = require('uuid');
|
2018-04-03 18:26:35 +08:00
|
|
|
|
2020-06-02 17:49:54 +08:00
|
|
|
module.exports = function initLogger ({ logger }) {
|
|
|
|
return function initLoggerMiddleware (req, res, next) {
|
2020-06-02 01:18:15 +08:00
|
|
|
const id = req.get('X-Request-Id') || uuid.v4();
|
2020-06-02 17:49:54 +08:00
|
|
|
res.locals.logger = logger.child({ id });
|
2020-06-02 01:18:15 +08:00
|
|
|
|
2020-06-03 20:28:35 +08:00
|
|
|
res.locals.logger.info({ request: req });
|
|
|
|
res.on('finish', () => res.locals.logger.info({ response: res }));
|
2020-06-06 02:08:40 +08:00
|
|
|
res.on('close', () => res.locals.logger.info({ end: true }));
|
2018-04-03 18:26:35 +08:00
|
|
|
|
2020-06-02 01:18:15 +08:00
|
|
|
next();
|
|
|
|
};
|
2018-04-03 18:26:35 +08:00
|
|
|
};
|