If you use Node 14 and above, you can use the built-in AsyncLocalStorage. Logging with IDs is a typical use-case of this new API. Here's an example of the use-case:
const { AsyncLocalStorage } = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();
function logWithId(msg) {
const id = asyncLocalStorage.getStore();
logger.log(id, msg);
}
asyncLocalStorage.run(uuid(), () => {
consumerGroup.on('message',function(msgc){
program2.func2(msgc);
});
})
// ------Program2.ts
function func2(uuid,msgc){
logWithId(msgc);
program3.func3(msgc);
}
// -----------program3.ts
function func(uuid,msgc){
logWithId(msgc);
}
Read the docs to understand more about it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…