最近Log4js 升级到3以上的版本,发现在Cluste模式下 没有日志文件生成,在网上找了看到一篇文章“探索 PM2 Cluster 模式下 Log4js 日志丢失(https://juejin.im/post/5b7ea1e56fb9a01a0f24979a?utm_medium=hao.caibaojian.com&utm_source=hao.caibaojian.com)”
但是上面是在PM2的情况下,
我并没有使用,但是看到作者贴出的源码
// clear out the listeners, because configure has been called.
listeners.length = 0;
disabled = config.disableClustering;
pm2 = config.pm2;
pm2InstanceVar = config.pm2InstanceVar || 'NODE_APP_INSTANCE';
debug(`clustering disabled ? ${disabled}`);
debug(`cluster.isMaster ? ${cluster.isMaster}`);
debug(`pm2 enabled ? ${pm2}`);
debug(`pm2InstanceVar = ${pm2InstanceVar}`);
debug(`process.env[${pm2InstanceVar}] = ${process.env[pm2InstanceVar]}`);
// just in case configure is called after shutdown.
if (pm2) {
process.removeListener('message', receiver);
}
if (cluster.removeListener) {
cluster.removeListener('message', receiver);
}
if (config.disableClustering) {
debug('Not listening for cluster messages, because clustering disabled.');
} else if (isPM2Master()) {
// PM2 cluster support
// PM2 runs everything as workers - install pm2-intercom for this to work.
// we only want one of the app instances to write logs.
debug('listening for PM2 broadcast messages');
process.on('message', receiver);
} else if (cluster.isMaster) {
debug('listening for cluster messages');
cluster.on('message', receiver);
} else {
debug('not listening for messages, because we are not a master process.');
}
});
中有一句disabled = config.disableClustering;
就猜想是不是这个参数影响,就在
log4js.configure({
disableClustering: true,
...
})
然后日志文件就生成了
区网上查找了一下log4js API 对这个字段的解析是
disableClustering (boolean, 可选):
true:使用的 log4js 忽略集群环境(clustered environments)或者你在 pm2 日志中遇到了麻烦。每一个工作进程都将进行自己的日志记录。请小心使用这里如果你要把日志记录输出到文件。
#参考链接#
[1]: https://juejin.im/post/5b7ea1e56fb9a01a0f24979a?utm_medium=hao.caibaojian.com&utm_source=hao.caibaojian.com
[1]: https://blog.csdn.net/wonder233/article/details/80738658