Skip to content

Commit 329afeb

Browse files
committed
feat: add MICROAPP_LOGGER_LEVEL 日志等级配置,默认全开为 5
1 parent aed692f commit 329afeb

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/plugins/factory/_customResult.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ module.exports = function(app) {
6666
}
6767

6868
this.logger.printf('Response Result', `${this.method} ${this.path}`, '\n',
69-
JSON.stringify(json, null, 4)
70-
.split('\n')
71-
.slice(0, 20)
72-
.join('\n')
69+
JSON.stringify(Object.keys(json).reduce((obj, key) => {
70+
obj[key] = JSON.stringify(json[key]);
71+
return obj;
72+
}, {}), null, 4)
7373
);
7474
this.body = json;
7575
};

src/plugins/factory/_koaLogger.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,50 @@ module.exports = function(app) {
88
const config = app.$config || {};
99
const NAME = config.name || config.info.name || CONSTANTS.NAME;
1010

11+
// 日志等级
12+
const LOGGER_LEVEL = process.env.MICROAPP_LOGGER_LEVEL || 5;
13+
1114
const customLogger = {
1215
debug(...args) {
16+
if (Number(LOGGER_LEVEL) <= 0) {
17+
return;
18+
}
1319
const time = getTime();
1420
logger.debug(`[${time}]`, ...args);
1521
},
1622
error(...args) {
23+
if (Number(LOGGER_LEVEL) <= 1) {
24+
return;
25+
}
1726
const time = getTime();
1827
logger.error(`[${time}]`, ...args);
1928
},
2029
warn(...args) {
30+
if (Number(LOGGER_LEVEL) <= 2) {
31+
return;
32+
}
2133
const time = getTime();
2234
logger.warn(`[${time}]`, ...args);
2335
},
2436
info(...args) {
37+
if (Number(LOGGER_LEVEL) <= 4) {
38+
return;
39+
}
2540
const time = getTime();
2641
logger.info(`[${time}]`, ...args);
2742
},
2843
success(...args) {
44+
if (Number(LOGGER_LEVEL) <= 4) {
45+
return;
46+
}
2947
const time = getTime();
3048
logger.success(`[${time}]`, ...args);
3149
},
3250

3351
printf(name = '', ...args) {
52+
if (Number(LOGGER_LEVEL) <= 4) {
53+
return;
54+
}
3455
const time = getTime();
3556
const title = `[${time}] | ${name}`;
3657
const len = title.length;

0 commit comments

Comments
 (0)