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

Posted

技术标签:

【中文标题】Winston - MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏【英文标题】:Winston - MaxListenersExceededWarning: Possible EventEmitter memory leak detected 【发布时间】:2020-02-27 10:09:26 【问题描述】:

在使用带有 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);

仍然面临同样的问题。

【问题讨论】:

修复添加无限监听的代码 @gman 代码用于注释掉“new winston.transports.File( filename: 'combined.log' )” 对于遇到此问题的任何人,请参阅this ticket。 【参考方案1】:

我遇到了一个相关案例,标题是上面提到的警告,所以只是将我的调查中的见解分享给可能进入此线程的其他人:

弹出这个警告的原因是什么?好吧,this comment in this relevant thread 解释得很好。简而言之:运输的广泛使用。 如何禁用警告?我发现很少有地方(如this one)提供更新最大侦听器。他们使用process.setMaxListeners(0);,这与问题中使用的不同。 应该禁用警告吗?我倾向于不(如first comment in this thread 中所述)。 为什么 clear() 有帮助?好吧,我没有阅读太多关于它的内容,但根据 documnation - 它删除了所有传输。 在哪些其他情况下可能会发生此错误? - 当使用传输创建大量记录器实例时。我就是这样。

【讨论】:

这应该是答案。它不仅更具描述性和解释性,还为每个可能的解决方案提供了上下文。【参考方案2】:

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

【讨论】:

【参考方案3】:

如下图设置handleExceptions: false

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

【讨论】:

以上是关于Winston - MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章

Winston 不显示错误详细信息

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

如何让 Winston 使用 Webpack?

winston香烟多少钱

winston'thandExceptions:true'记录两次

Winston/Node.js 如何只为特定环境添加传输?