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 01:18:15 +08:00
|
|
|
module.exports = function logger () {
|
|
|
|
return function loggerMiddleware (req, res, next) {
|
|
|
|
const id = req.get('X-Request-Id') || uuid.v4();
|
|
|
|
res.locals.logger = global.logger.child({ id });
|
|
|
|
|
|
|
|
res.locals.logger.info(req);
|
|
|
|
res.on('finish', () => res.locals.logger.info(res));
|
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
|
|
|
};
|