-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-hook.js
executable file
·69 lines (65 loc) · 2.05 KB
/
console-hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require('colors');
const child_process = require('child_process');
const _log = console.log;
const _info = console.info;
const _warn = console.warn;
const _error = console.error;
function stringify(obj) {
switch (obj && obj.constructor) {
case String: {
return obj;
}
case Function: {
return `[Function: ${obj.name || 'anonymous'}]`;
}
case Error: {
return obj.stack;
}
}
try {
return JSON.stringify(obj, (key, value) => {
if (value && value.constructor === Function) {
return `[Function: ${value.name || 'anonymous'}]`;
}
if (value && value.constructor === RegExp) {
return value.toString();
}
return value;
}, 2);
} catch (error) {
return obj.toString();
}
}
function consoleHook() {
console.log = (...params) => {
let color = 'brightGreen';
_log('[V]'[color], ...(params.map(it => stringify(it)[color])));
};
console.info = (...params) => {
let color = 'brightMagenta';
_info('[I]'[color], ...(params.map(it => stringify(it)[color])));
};
console.warn = (...params) => {
let color = 'yellow';
_warn('[W]'[color], ...(params.map(it => stringify(it)[color])));
};
console.error = (...params) => {
let color = 'brightRed';
_error('[E]'[color], ...(params.map(it => stringify(it)[color])));
};
if (process.platform.match(/win/)) { // win32
child_process.execSync('chcp 65001'); // 解决Windows下输出乱码问题
}
}
void function test() {
consoleHook();
console.log('ABC', [1, 2, 3], new Error('测试颜色'));
console.info('ABC', [1, 2, 3], new Error('测试颜色'));
console.warn('ABC', [1, 2, 3], new Error('测试颜色'));
console.error('ABC', [1, 2, 3], new Error('测试颜色'));
}
// ();
module.exports = {
_log, _info, _error, _warn,
consoleHook,
};