当在nodejs中使用集群和winston时,日志的maxsize不起作用

时间:2013-09-17 04:30:49

标签: node.js cluster-computing winston

当我在nodejs中使用集群和winston时,日志的maxsize不起作用。所有日志都会记录一个文件,尽管其大小与maxsize相同。 在这里看到同样的问题: https://github.com/flatiron/winston/issues/275

2 个答案:

答案 0 :(得分:9)

显而易见的解决方案是:只让主人登录文件。

var winston = require('winston');
var cluster = require('cluster');

if(cluster.isMaster) {
    cluster.setupMaster({ silent: true }); // Keep cluster from automatically grabbing stdin/out/err
    for(var i = 0; i < 4; i++) {
        cluster.fork();
    }
    winston.add(winston.transport.File, { filename: 'log.txt' });
    cluster.workers.forEach(function(worker, i) {
        worker.process.stdout.on('data', function(chunk) {
            winston.info('worker ' + i + ': ' + chunk);
        });
        worker.process.stderr.on('data', function(chunk) {
            winston.warn('worker ' + i + ': ' + chunk);
        });
    });
} else {
    // Leave winston alone and only log to stdout/err in the workers
}

然后只有一个进程保留文件描述符,因此您的日志轮换应该照常工作。

答案 1 :(得分:1)

也许您应该使用DailyFileRotation。 截至今天,我没有看到任何问题。我在主进程和分叉进程中使用winston,并且它们共享同一个文件。

这是我从测试中发现的。您可以看到所有日志在轮换之前和之后均匀分布。您还可以清楚地看到日志中的截止日期。转换后旧日志也停止增长。

joephone@Fengs-MacBook-Pro-2:logs$ ls
total 3184
-rw-r--r--  1 joephone  staff  985111 Aug 24 20:01 info.log.2016-08-25-03-00
-rw-r--r--  1 joephone  staff  641506 Aug 24 20:01 info.log.2016-08-25-03-01
joephone@Fengs-MacBook-Pro-2:logs$ cut -f8 -d"\"" info.log.2016-08-25-03-00 | sort | uniq -c
3299 65151
3355 65152
3307 65153
3345 65154
   1 cpucount:4
   1 worker 65151 starts working.
   1 worker 65152 starts working.
   1 worker 65153 starts working.
   1 worker 65154 starts working.
joephone@Fengs-MacBook-Pro-2:logs$ cut -f8 -d"\"" info.log.2016-08-25-03-01 | sort | uniq -c
2193 65151
2207 65152
2122 65153
2147 65154
joephone@Fengs-MacBook-Pro-2:logs$ tail -3 info.log.2016-08-25-03-00
{"level":"info","message":"65154","timestamp":"2016-08-25T03:00:59.997Z"}
{"level":"info","message":"65153","timestamp":"2016-08-25T03:00:59.997Z"}
{"level":"info","message":"65152","timestamp":"2016-08-25T03:00:59.997Z"}
joephone@Fengs-MacBook-Pro-2:logs$ head -3 info.log.2016-08-25-03-01
{"level":"info","message":"65151","timestamp":"2016-08-25T03:01:00.000Z"}
{"level":"info","message":"65151","timestamp":"2016-08-25T03:01:00.012Z"}
{"level":"info","message":"65152","timestamp":"2016-08-25T03:01:00.006Z"}

为了我的好奇心,我还通过将所有代码放入每个分叉进程进行测试。我仍然没有看到任何问题。

joephone@Fengs-MacBook-Pro-2:logs$ ls
total 3216
-rw-r--r--  1 joephone  staff  993838 Aug 24 20:14 info.log.2016-08-25-03-13
-rw-r--r--  1 joephone  staff  650312 Aug 24 20:14 info.log.2016-08-25-03-14
joephone@Fengs-MacBook-Pro-2:logs$ cut -f8 -d"\"" info.log.2016-08-25-03-13 | sort | uniq -c
3402 65755
3342 65756
3344 65757
3337 65758
   1 worker 65755 starts working.
   1 worker 65756 starts working.
   1 worker 65757 starts working.
   1 worker 65758 starts working.
joephone@Fengs-MacBook-Pro-2:logs$ cut -f8 -d"\"" info.log.2016-08-25-03-14 | sort | uniq -c
2183 65755
2202 65756
2230 65757
2173 65758
joephone@Fengs-MacBook-Pro-2:logs$ tail -3 info.log.2016-08-25-03-13 
{"level":"info","message":"65757","timestamp":"2016-08-25T03:13:59.986Z"}
{"level":"info","message":"65755","timestamp":"2016-08-25T03:13:59.986Z"}
{"level":"info","message":"65756","timestamp":"2016-08-25T03:13:59.994Z"}
joephone@Fengs-MacBook-Pro-2:logs$ head -3 info.log.2016-08-25-03-14
{"level":"info","message":"65758","timestamp":"2016-08-25T03:14:00.005Z"}
{"level":"info","message":"65757","timestamp":"2016-08-25T03:14:00.005Z"}
{"level":"info","message":"65758","timestamp":"2016-08-25T03:14:00.024Z"}

出于好奇心的好奇心,我甚至测试了它,错误地导致分叉过程,主人带来了一个新的。日志仍然有效。

实际上这两种方式可能没有什么不同,如果你使用ps来grep你正在运行的脚本,你将看到主进程和分叉进程。它们与我认为在多个终端中运行它们非常相似。