fetch-dht/lib/log/index.js

24 lines
566 B
JavaScript
Raw Permalink Normal View History

2020-04-12 21:33:32 +00:00
const uuid = require('uuid');
module.exports = {};
const log = ({ time, ...args }) => {
console.log(
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 };