winston logger includes handling uncaught promise rejections in exception handler

Viewed 166

I am learning a set of Node backend tools. I just added winston and winston-mongodb logging. I am hoping someone with more experience with winston can clarify whether the uncaught exceptional handler is assumed to also deal with unhandled promises. From the docs, I assumed these were designed as two separate, distinct handlers. In which case, I am rather stuck on a bug. I cannot find a way to separate out unhandled promise rejections from general uncaught exceptions. I can essentially create a more narrow, duplicate log for only unhandled promises. But those unhandled promises will be included with the uncaught exception logs. I spent some time logging the transports, but I could turn up any obvious reasons in the way I set up the transports for what seems like duplication. I am wondering at this point if this is expected behavior.

Thanks!

1 Answers

Not sure if this is a bug in the Winston library or not, but I may have found a workaround when experiencing a similar issue.

When writing both the exception handlers like this:

  // log unhandled exceptions.
  exceptionHandlers: [
    new transports.File({
      filename: "./logs/exceptions.log",
    }),
  ],
  
  // log unhandled rejections.
  rejectionHandlers: [
    new transports.File({
      filename: "./logs/rejections.log",
    }),
  ],

This caused a duplicate entry in the error log, where I believe the expected result is to only have one error logged - being either exception or rejection, whichever tripped up.

The solution to this for me was to write the exception handlers like this:

  transports: [
    new transports.File({
      filename: "./logs/errors.log",
      handleExceptions: true, /* set this option to true */
      handleRejections: true, /* set this option to true */
    }),
  ],

This should log both exceptions and rejections to the same log file.

Related