24 lines
577 B
JavaScript
24 lines
577 B
JavaScript
const uuid = require('uuid');
|
|
|
|
module.exports = {};
|
|
|
|
const log = ({ time, ...args }) => {
|
|
console.log(
|
|
JSON.stringify({ time: new Date().toISOString(), requestId, ...args }),
|
|
);
|
|
};
|
|
|
|
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 };
|