node-pinus / pinus

A fast,scalable,distributed game server framework for Node.js, Powered by TypeScript. 一个TypeScript写的node.js分布式游戏/应用服务器框架(原型基于pomelo)。
https://pinus.io
MIT License
1.83k stars 379 forks source link

已知 logger 存在内存泄漏的问题 #167

Closed bruce48x closed 3 years ago

bruce48x commented 3 years ago

log4js 的文档提到了大量写入的情况下,会有内存泄漏的问题

https://log4js-node.github.io/log4js-node/file.html

If your application logs a large volume of messages, and find memory usage increasing due to buffering log messages before being written to a file, then you can listen for “log4js:pause” events emitted by the file appenders. Your application should stop logging when it receives one of these events with a value of true and resume when it receives an event with a value of false.

解决思路是在 pinus-logger 里监听 log4js:pause 事件,在 value 为 true 时停止写入,在 value 为 false 时恢复写入

参考代码

pinus-logger/lib/logger.ts

function getLogger(...args: string[]) {
    // ... 省略前面的代码
    // 通过 pause 变量来 开/关 日志
    let pause = false;
    process.setMaxListeners(0);
    process.on('log4js:pause', (val) => {
        pause = val;
    });

    ['log', 'debug', 'info', 'warn', 'error', 'trace', 'fatal'].forEach((item, idx) => {
        pLogger[item] = function () {
            if (pause) {
                return;
            }
            // ... 省略后面的代码
        };
    });
    return pLogger as log4js.Logger;
}
whtiehack commented 3 years ago

防御加上了. 感谢分享哈.