Skip to content

Commit 0986212

Browse files
committed
增加对文件追加写入的try catch 避免记录log的时候抛出异常,影响到原有函数的正常执行
1 parent aa5fde6 commit 0986212

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@open-node/logger",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "log recorder, info and error",
55
"main": "src/logger.js",
66
"scripts": {

‎src/logger.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ function Logger({ errorLogPath, infoLogPath, ignoreErrors }, { _ }, clientId) {
4141
if (e.data) content.push(JSON.stringify(e.data));
4242
if (extra != null) content.push(JSON.stringify(extra));
4343
if (e.stack) content.push(JSON.stringify(e.stack));
44-
fs.appendFileSync(file, `${content.join("\t")}\n`);
44+
try {
45+
fs.appendFileSync(file, `${content.join("\t")}\n`);
46+
} catch (err) {
47+
console.error("Logger.error appendFileSync faid: %o", err);
48+
}
4549
};
4650

4751
/**
@@ -61,7 +65,11 @@ function Logger({ errorLogPath, infoLogPath, ignoreErrors }, { _ }, clientId) {
6165
const file = `${dir}/info.log`;
6266
const content = [time, clientId, message];
6367
if (extra != null) content.push(JSON.stringify(extra));
64-
fs.appendFileSync(file, `${content.join("\t")}\n`);
68+
try {
69+
fs.appendFileSync(file, `${content.join("\t")}\n`);
70+
} catch (e) {
71+
console.error("Logger.info appendFileSync faid: %o", e);
72+
}
6573
};
6674

6775
/**

0 commit comments

Comments
 (0)