Winston-MaxListenersExceededWarning:可能检测到EventEmitter内存泄漏

时间:2019-11-01 03:14:29

标签: node.js winston

在使用带有winston-daily-rotate-file软件包的文件日志记录时,在winston中出现以下错误,代码在Winston控制台日志记录中很好地工作,

  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)

试图将最大侦听器设置为无穷大,

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

仍然面临同样的问题。

3 个答案:

答案 0 :(得分:2)

我遇到了一个相关案例,并在标题中提到了警告,因此,我只是将我的调查中的见解分享给可能会参与此话题的其他人:

  • 此警告弹出的原因是什么?好,this comment in this relevant thread很好地说明了这一点。简而言之:广泛使用交通工具。
  • 如何禁用警告??我发现很少有地方(例如this one)可以更新最大监听器。他们使用process.setMaxListeners(0);,与问题中使用的语言不同。
  • 是否应该禁用该警告?我倾向于否(如first comment in this thread中所述)。
  • 为什么clear()有帮助?好吧,我对它的了解不多,但是根据documnation-它删除了所有传输。
  • 在其他情况下是否可能发生此错误?-当人们使用传输创建大量记录器实例时。对我来说就是这种情况。

答案 1 :(得分:0)

我遇到了同样的问题,在完成记录器后,通过调用winstonLoggerInstance.clear()来解决此问题。 clear()清除所有传输。

答案 2 :(得分:-1)

如下所示设置handleExceptions: false

createLogger({
    level:  'info',
    transports: [
      new transports.Console({
        format: format.combine(
          format.colorize(),
          format.simple(),
          myFormat
      ),
        handleExceptions: false
      })
    ],
    format: logFormat(label)
  })