2020-04-12 21:33:32 +00:00
|
|
|
const uuid = require('uuid');
|
|
|
|
|
|
|
|
module.exports = {};
|
|
|
|
|
|
|
|
const log = ({ time, ...args }) => {
|
|
|
|
console.log(
|
2020-04-13 12:39:46 +00:00
|
|
|
JSON.stringify({ time: new Date().toISOString(), ...args }),
|
2020-04-12 21:33:32 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const traced = (requestId) => ({ requestId: _, ...args }) =>
|
|
|
|
log({ requestId, ...args });
|
|
|
|
|
|
|
|
const middleware = (req, res, next) => {
|
|
|
|
// generate (or use existing) requestId
|
|
|
|
req.requestId = req.requestId || uuid.v4();
|
|
|
|
// set a tracing logger use elsewhere
|
|
|
|
req.log = traced(req.requestId);
|
|
|
|
req.log({ status: 'new' });
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { log, traced, middleware };
|