8

Got below error in winston when using file logging with winston-daily-rotate-file package, code works well with winston console logging,

  MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit
        at _addListener (events.js:256:17)
        at DailyRotateFile.addListener (events.js:272:10)
        at DailyRotateFile.once (events.js:301:8)
        at asyncForEach (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:217:17)
        at /home/ubuntumachine/Desktop/project/node_modules/async/internal/withoutIndex.js:9:16
        at eachOfArrayLike (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:65:9)
        at exports.default (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:9:5)
        at eachLimit (/home/ubuntumachine/Desktop/project/node_modules/async/forEach.js:80:24)
        at ExceptionHandler._uncaughtException (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:203:5)
        at process.emit (events.js:198:13)
    (node:20805) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 finish listeners added. Use emitter.setMaxListeners() to increase limit
        at _addListener (events.js:256:17)
        at DailyRotateFile.addListener (events.js:272:10)
        at DailyRotateFile.once (events.js:301:8)
        at asyncForEach (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:216:17)
        at /home/ubuntumachine/Desktop/project/node_modules/async/internal/withoutIndex.js:9:16
        at eachOfArrayLike (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:65:9)
        at exports.default (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:9:5)
        at eachLimit (/home/ubuntumachine/Desktop/project/node_modules/async/forEach.js:80:24)
        at ExceptionHandler._uncaughtException (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:203:5)
        at process.emit (events.js:198:13)
    (node:20805) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 31 error listeners added. Use emitter.setMaxListeners() to increase limit
        at _addListener (events.js:256:17)
        at Console.addListener (events.js:272:10)
        at Console.once (events.js:301:8)
        at asyncForEach (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:217:17)
        at /home/ubuntumachine/Desktop/project/node_modules/async/internal/withoutIndex.js:9:16
        at eachOfArrayLike (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:65:9)
        at exports.default (/home/ubuntumachine/Desktop/project/node_modules/async/eachOf.js:9:5)
        at eachLimit (/home/ubuntumachine/Desktop/project/node_modules/async/forEach.js:80:24)
        at ExceptionHandler._uncaughtException (/home/ubuntumachine/Desktop/project/node_modules/winston/lib/winston/exception-handler.js:203:5)
        at process.emit (events.js:198:13)

Tried to set max listeners to infinity,

const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.setMaxListeners(Infinity);

still facing same issue.

Purushoth.Kesav
  • 615
  • 7
  • 16

4 Answers4

4

I came accross a related case with the mentioned warning the headline, so just sharing insights from my investigation for others who might get into this thread:

  • What is the reason this warning pops up? well, this comment in this relevant thread explains it well. In short: extensive usage of transports.
  • How to disable the warning? I found few places (like this one) offering to update the max listener. They use process.setMaxListeners(0);, which is different from what used in the question.
  • Should one disable the warning? I tend toward no (as explained in the first comment in this thread).
  • Why clear() helped? well, I didn't read much about it, yet according to the documnation - It remove all transports.
  • In what other cases this error might occur? - when one creates a lot of logger instances using transport. That was the case for me.
Maoritzio
  • 1,142
  • 2
  • 13
  • 31
  • This should be the answer. Not only is it more descriptive and explanatory, it also provided context for each possible solution. – GaidinD Feb 23 '21 at 18:24
2

I had the same issue, it got fixed by calling winstonLoggerInstance.clear() when I was done with the logger. clear() clears all the transports.

Ehsan
  • 1,022
  • 11
  • 20
0

Set handleExceptions: false as shown below

createLogger({
    level:  'info',
    transports: [
      new transports.Console({
        format: format.combine(
          format.colorize(),
          format.simple(),
          myFormat
      ),
        handleExceptions: false
      })
    ],
    format: logFormat(label)
  })
Sunday Aroh
  • 21
  • 1
  • 4
  • This is a good solution if you don't need to capture unhandled exceptions. You can see the event listener is added here: https://github.com/winstonjs/winston/blob/f707f337122ad629aa1033ff935fe1b5e9aed170/lib/winston/exception-handler.js#L40-L53 – mrcrowl Jul 07 '22 at 08:03
0

I had a similar case leading to:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 uncaughtException listeners added to [process]. Use emitter.setMaxListeners() to increase limit

As for Maoritzio, in my case this error was caused by creating more than 10 loggers (one for each test class...).

So the correct fix was to repair my own local logging class, so that it correctly obtained the same shared instance (instead of re-creating too many instances).

The cause for the error message is in the lines pointed to by @mccrowl:

    process.on('uncaughtException', this.catcher);

The process variable comes from the Node Process API. It is an instance of an EventEmitter, which has the setMaxListeners method, with the following comment:

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

Thus, if you know you need more than 10 loggers, and you know how many, you could change the maximum number of listeners on the global process (for all types of events):

    process.setMaxListeners(25)

AFAIK there is no specific method to set this maximum for specific events, like the uncaughtException event used by Winston.

The Winston close method neatly removes these handlers. So in my case, adding logger.close() in the tear-down method of my test class also did the trick.

avandeursen
  • 8,458
  • 3
  • 41
  • 51