@anjianshi/utils 2.4.9 → 2.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,9 +50,9 @@ export declare class FileHandler extends LogHandler {
50
50
  private buffer;
51
51
  private bufferSize;
52
52
  protected pushBuffer(...strings: string[]): void;
53
- protected flush(): void;
53
+ protected flush(sync?: boolean): void;
54
54
  protected initFlush(): void;
55
55
  get filepath(): string;
56
56
  protected initLogDir(): void;
57
- protected write(content: string): void;
57
+ protected write(content: string, sync?: boolean): void;
58
58
  }
@@ -112,19 +112,21 @@ export class FileHandler extends LogHandler {
112
112
  if (this.options.flushInterval === 0 || this.bufferSize >= this.options.flushLength)
113
113
  this.flush();
114
114
  }
115
- flush() {
115
+ flush(sync) {
116
116
  if (this.buffer.length) {
117
117
  const content = this.buffer.join('');
118
118
  this.buffer = [];
119
119
  this.bufferSize = 0;
120
- this.write(content);
120
+ this.write(content, sync);
121
121
  }
122
122
  }
123
123
  initFlush() {
124
124
  if (this.options.flushInterval !== 0) {
125
125
  setInterval(() => this.flush(), this.options.flushInterval);
126
126
  }
127
- process.on('exit', () => this.flush());
127
+ // 进程退出前把尚未写入文件的日志强制写入
128
+ // 这里必须用同步的方式来写,不然会写入不进去(可能是因为异步的话是放到下一个事件循环,但进程在这个事件循环内就退出了)
129
+ process.on('exit', () => this.flush(true));
128
130
  }
129
131
  // 文件系统交互 File system interaction
130
132
  get filepath() {
@@ -135,10 +137,15 @@ export class FileHandler extends LogHandler {
135
137
  if (!fs.existsSync(this.options.dir))
136
138
  fs.mkdirSync(this.options.dir);
137
139
  }
138
- write(content) {
139
- fs.appendFile(this.filepath, content, error => {
140
- if (error)
141
- console.error('[logger] write failed: ' + String(error));
142
- });
140
+ write(content, sync = false) {
141
+ if (sync) {
142
+ fs.appendFileSync(this.filepath, content);
143
+ }
144
+ else {
145
+ fs.appendFile(this.filepath, content, error => {
146
+ if (error)
147
+ console.error('[logger] write failed: ' + String(error));
148
+ });
149
+ }
143
150
  }
144
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.4.9",
3
+ "version": "2.4.10",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -28,10 +28,10 @@
28
28
  "typescript": "^5.6.3",
29
29
  "vconsole": "^3.15.1",
30
30
  "@anjianshi/presets-eslint-node": "4.0.13",
31
+ "@anjianshi/presets-eslint-react": "4.0.12",
31
32
  "@anjianshi/presets-eslint-typescript": "5.0.10",
32
33
  "@anjianshi/presets-prettier": "3.0.1",
33
- "@anjianshi/presets-typescript": "3.2.3",
34
- "@anjianshi/presets-eslint-react": "4.0.12"
34
+ "@anjianshi/presets-typescript": "3.2.3"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@emotion/react": "^11.13.3",