node.js - Winston - MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏
问题描述
当使用带有 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);
仍然面临同样的问题。
解决方案
我遇到了一个相关案例,上面提到的警告是标题,所以只是分享我的调查见解,以供其他可能进入该线程的人使用:
- 弹出此警告的原因是什么?好吧,这个相关线程中的评论很好地解释了这一点。简而言之:运输的广泛使用。
- 如何禁用警告?我发现很少有地方(比如这个)提供更新最大监听器。他们使用
process.setMaxListeners(0);
,这与问题中使用的不同。 - 应该禁用警告吗?我倾向于不(如该线程的第一条评论中所述)。
- 为什么 clear() 有帮助?好吧,我没有阅读太多关于它的内容,但根据文档- 它删除了所有传输。
- 在其他什么情况下可能会发生此错误?- 当使用传输创建大量记录器实例时。我就是这样。
推荐阅读
- django - 在 django 中对用户进行身份验证
- android - Android APK:使用 Android Studio 调试 APK
- c - 设置全局字符的 C 函数*
- c - C - 按输出顺序打印的问题
- linux - 为什么在根文件夹中拥有 777 权限不允许我登录?
- c++ - 使用 Googlemock 进行依赖注入和模拟的实用示例
- kubernetes - 使用 nvim-lspconfig 为自动完成设置 yaml.schema “kubernetes”
- typescript - RxJS switchMap/mergeMap 导致 Obserable
- python - 在 docker 容器内设置环境路径
- php - I'm getting an error when trying to make an SMTP connection with PHP?