nestjs-winston-logger icon indicating copy to clipboard operation
nestjs-winston-logger copied to clipboard

Enhance middleware more easily for different logger

Open Marcotsept opened this issue 3 years ago • 0 comments

I found another issue as well. In addition to having different loggers, I want to have a request id across all logs. There is a great method to do this for a global log but if I want to do it for specific logs, my first approach is:

const requestLogger = new NestjsWinstonLoggerService({
    format:format.simple(),
    transports: [
      new transports.File({ filename: "request.log",
      format: format.printf(log => log.message.trim()) }),
    ],
  });

  let authLog = app.get(getLoggerToken(AUTH_LOG));

  app.use(appendIdToRequest);

  app.use(appendRequestIdToLogger(requestLogger));
  app.use(appendRequestIdToLogger(authLog));
  
  configMorgan.appendMorganToken("reqId", TOKEN_TYPE.Request, "reqId");
  app.use(morganRequestLogger(requestLogger));
  app.use(morganResponseLogger(requestLogger));

The issue here is that I'm now exposing some of the internals through getLoggerToken. I think there should be a better way to do this but I haven't thought much about it yet.

This issue may adopt to use Nestjs middleware rather than global middleware. We can open another issue for further discussion. For you reference - https://docs.nestjs.com/middleware

Originally posted by @Marcotsept in https://github.com/owen1002/nestjs-winston-logger/issues/8#issuecomment-999239861

Marcotsept avatar Dec 22 '21 02:12 Marcotsept