First part here Basic Logging & Debugging in Microservices - Part 1
In previous post, I have talked about the advantages of building a custom Logger module that can group all the related log data into one single log entry. In this post, I will continue discussing about some basic ideas to integrate it into the Microservices architecture and organise all the log data for better investigation.
Integrate custom MyLogger into your application
Integrating the custom MyLogger module into the application is a quite straightforward task. Instead of manually initialising and flushing the logs, you will need to implement a wrapper or higher order function to do that automatically. For example, if you are using Koa.js to for your http service, simply wrap the requests inside a logger middleware like this
const MyLogger = require('./my-logger.js')
// initialise koa app
// ...
function* myLoggerMdw(next) {
// init logger and assign to the context
const metadata = {
appName: 'your-service-name',
routeName: this.request.routeName
};
const logger = new MyLogger(metadata);
this.logger = logger;
// wrap logger around your request
try {
logger.push('info', 'Start request', this.request.headers);
yield next;
logger.push('info', 'Request success', this.status);
logger.write();
} catch(e) {
if (e.status < 500) {
logger.push('warn', 'Handled error', e.message);
} else {
logger.push('error', 'Unhandled error', e.message);
}
logger.write();
throw e;
}
}
// custom middleware for MyLogger
app.use(myLoggerMdw)