1

我使用Bunyan,然后我尝试Winston,我得到了与它说的相同的结果

EMFILE:打开的文件太多,打开

这样我就明白我做错了什么。我尝试测试的是

var winston = require('winston');

setInterval(function(){

var logger1 = new (winston.Logger)({
       level: 'info',
    transports: [
      new (winston.transports.Console)({ level: 'error' }),
      new (winston.transports.File)({ filename: './logs/1.log', level: 'error' })
    ]
});

var logger2 = new (winston.Logger)({
       level: 'info',
    transports: [
      new (winston.transports.Console)({ level: 'error' }),
      new (winston.transports.File)({ filename: './logs/2.log', level: 'error' })
    ]
});

logger1.error('Hello logs1');
logger2.error('Hello logs2');

}, 50);

注意:这只是一个测试。我将记录器的创建放在 setInterval 中进行测试。如何使它工作?

我想要的是

Create logger1
Dispose logger1
Create logger2
Dispose logger2
Create logger1
Dispose logger1
Create logger1
Dispose logger1

...

现在是什么我想

Create logger1
Create logger2
Create logger1
Create logger2
Create logger1
Create logger2

...

4

1 回答 1

0

EMFILE: too many open files (with Winston v3.2.1)

As you create a winston instance, it will open the file and never close it until logger instance exist. Close() function worked like a champ. We need to use close() function to remove instance of winston.

   logger.close();

In you case you need to include two extra lines in your code.

logger1.error('Hello logs1');
logger1.close();

logger2.error('Hello logs2');
logger2.close();
于 2020-07-15T11:36:19.630 回答