fetch-dht/lib/log/index.js
2020-04-12 16:33:32 -05:00

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 };