Log correlation for Moleculer via AsyncLocalStorage
Adds the current context's requestID
to any log messages made while the context is available. Takes
advantage of Node 14+ AsyncStorage
.
npm install moleculer-correlation --save
const { ServiceBroker } = require("moleculer");
const { CorrelationMiddleware } = require("moleculer-correlation");
const broker = new ServiceBroker({
middlewares: [CorrelationMiddleware]
});
// --- SERVICE ---
broker.createService({
name: "greeter",
actions: {
hello: {
handler(ctx) {
this.logger.info("Creating a Greeting")
return `Hello ${ctx.params.name}`;
}
}
}
});
// --- TEST ---
broker.start()
.then(() => broker.call("greeter.hello").then(res => broker.logger.info(res)))
// --- CONTEXT REQUEST ID IN LOGS WHEN AVAILABLE ---
// [2022-06-09T03:18:02.291Z] INFO node/GREETER: [2e524af4-69a1-440f-9a1d-1904a6b28258] Creating a Greeting
$ npm test
In development with watching
$ npm run ci
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.
The project is available under the MIT license.
Copyright (c) 2022 Adam McCormick