3

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

4

2 回答 2

9

显而易见的解决方案是:只让 master 登录到文件中。

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
}

然后只有一个进程持有文件描述符,所以你的日志轮换应该像往常一样工作。

于 2013-09-17T04:57:46.003 回答
2

也许您应该改用 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 你正在运行的脚本,你会看到主进程和分叉进程。我认为它们与您在多个终端中运行它们非常相似。

于 2016-08-25T03:06:03.467 回答