Skip to content

Commit 97ec1dd

Browse files
author
zhangjiajia04
committed
fix: Repair log file cutting in pm2 cluster mode
1 parent 2568951 commit 97ec1dd

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

packages/logger/src/transports/file.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as utility from 'utility';
55
import * as cluster from 'cluster';
66
import * as schedule from 'node-schedule';
77
import * as util from 'util';
8+
import { isMainProcess } from '../utils';
89
import Transport from './transport';
910
import { ITransportFileOption } from '../types/transport.t';
1011
import { TConsoleMeta } from '../types/console.t';
@@ -54,8 +55,9 @@ export default class FileTransport extends Transport {
5455
splitLog() {
5556
const self = this;
5657

57-
schedule.scheduleJob(this.splitTime, async () => {
58-
if (cluster.isMaster) {
58+
if (isMainProcess()) {
59+
// 主进程创建定时任务负责文件切割
60+
schedule.scheduleJob(this.splitTime, async () => {
5961
// 主进程通知各工作进程不要执行写入
6062
for (const id in cluster.workers) {
6163
if (cluster.workers[id]) {
@@ -67,14 +69,15 @@ export default class FileTransport extends Transport {
6769

6870
await new Promise((resolve, reject) => {
6971
const date = new Date();
70-
const time = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}-${date.getHours()}${date.getMinutes()}${date.getSeconds()}`;
72+
const time = `${date.getFullYear()}-${date.getMonth() + 1}
73+
-${date.getDate()}-${date.getHours()}${date.getMinutes()}${date.getSeconds()}`;
7174

7275
fs.exists(self.file, (exist) => {
73-
if (!exist) return resolve();
76+
if (!exist) return resolve(true);
7477
});
7578
fs.rename(self.file, `${self.file}.${time}`, (err) => {
7679
if (err) return reject(err);
77-
resolve();
80+
resolve(true);
7881
});
7982
});
8083
self.stream = self.createStream();
@@ -85,9 +88,8 @@ export default class FileTransport extends Transport {
8588
cluster.workers[id].send({ writable: true });
8689
}
8790
}
88-
}
89-
});
90-
if (cluster.isWorker) {
91+
});
92+
} else {
9193
process.on('message', (msg) => {
9294
if (msg.writable) {
9395
this.reload();
@@ -104,10 +106,10 @@ export default class FileTransport extends Transport {
104106
const stream = fs.createWriteStream(this.file, { flags: 'a', encoding: this.encoding });
105107

106108
const onError = (err: Error) => {
107-
console.error('%s ERROR %s [wf-logger] [%s] %s',
109+
console.error('%s ERROR %s [UMajs-logger] [%s] %s',
108110
utility.logDate(','), process.pid, this.file, err.stack);
109111
this.reload();
110-
console.warn('%s WARN %s [wf-logger] [%s] reloaded', utility.logDate(','), process.pid, this.file);
112+
console.warn('%s WARN %s [UMajs-logger] [%s] reloaded', utility.logDate(','), process.pid, this.file);
111113
};
112114

113115
// only listen error once because stream will reload after error

packages/logger/src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as os from 'os';
22
import * as util from 'util';
33
import * as utility from 'utility';
44
import chalk from 'chalk';
5+
import * as cluster from 'cluster';
56
import { TConsoleMeta } from './types/console.t';
67
import getLevel from './level';
78

@@ -112,3 +113,17 @@ export function colorfulLog(level: string, msg: string) {
112113

113114
return msg;
114115
}
116+
117+
/**
118+
* 判断是否是主进程,PM2集群模式时,默认以NODE_APP_INSTANCE==='0'作为唯一的日志切割进程
119+
* @returns isMaster
120+
*/
121+
export function isMainProcess() {
122+
let isMaster = false;
123+
124+
if (cluster.isMaster || process.env.NODE_APP_INSTANCE === '0') {
125+
isMaster = true;
126+
}
127+
128+
return isMaster;
129+
}

0 commit comments

Comments
 (0)