Logging Middleware Microservice


I am required to save logs into a MySQL database of each request and response made to the backend. The issue is that we are migrating to microservices architecture. The backend was made with NodeJS and Express, and it has a middleware that does this task. Currently, it has this middleware attached to each microservice.
Microservices with log middleware attached

I would like to isolate this middleware as its own microservice. The issue is that I don’t know how to redirect the traffic this way. This is how I would like to manage it:

Architecture with logging isolated

I would like to do it this way, because we can make changes or add features to the middleware without having to implement it in each microservice. This is the middleware’s code:

const connection = require("../database/db");

const viewLog = (req, res, next) => {
  const oldWrite = res.write,
    oldEnd = res.end,
    chunks = [],
    now = new Date();

  res.write = function (chunk) {
    oldWrite.apply(res, arguments);

  res.end = function (chunk, error) {
    if (chunk) chunks.push(chunk);

    const bodyRes = Buffer.concat(chunks).toString("utf8");

    connection.query("CALL HospitalGatifu.insertLog(?,?,?,?,?)", [
      `[${req.method}] - ${req.url}`,
      `${JSON.stringify(req.body) || "{}"}`,
      res.statusCode === 400 ? 1 : 0,

    oldEnd.apply(res, arguments);


module.exports = viewLog;

I think there might be a way to manage this with Nginx which is the reverse proxy that we are using. I would like to get an approach of how to change the logs middleware.


Perhaps you might want to take a look at the sidecar pattern which is used in microservice architectures for common tasks (like logging).

In short, a sidecar runs in a container besides your microservice container. One task of the sidecar could be intercepting network traffic and logging requests and responses (and a lot of other possible tasks). The major advantage of this pattern is that you don’t need to change any code in your microservices and you don’t have to manage traffic redirection yourself. The latter will be handled by the sidecar itself.

The disadvantage is that you are required to run your microservices containerized and use some kind of container orchestration solution. I assume this being the case since you are moving towards a microservices based application.

One question about the log service in between of the webapp and the NGNIX server. What if the logging services goes down for some reason, is it acceptable for the entire application to go down?

Answered By – KDW

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published