-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
140 lines (130 loc) · 5.1 KB
/
logger.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
let listener;
const setListener = item => listener = item;
function createLogger(relPath, filePath) {
const logs = [];
const stacks = [];
let lastTime = "";
let lastTimestamp = 0;
let lastIndex = 0;
const log = (type, ...p) => {
if (logs.length > 2000) {
logs.splice(0, 500);
stacks.splice(0, 500);
}
const msg = p.map(o => typeof o === 'object' && o.message && o.stack && o.stack.length ? (o.logger ? o.stack : modifyStack(o.stack, relPath, filePath, true, true)) : o)
.join(', ');
const a = relPath !== './' ? `${relPath} ${msg}` : msg;
const t = new Date();
const _ = i => i < 10 ? "0" + i : i;
const __ = i => i < 100 ? (i < 10 ? "00" : "0") + i : i;
const n = t.getTime();
const i = n - lastTimestamp;
lastTimestamp = n;
const s = _(t.getSeconds());
const ms = __(t.getMilliseconds());
let f;
if (i < 1000) {
f = `${__(i)}`;
} else if (i < 10000) {
f = `${Math.floor(i / 1000)}k${Math.floor(i / 100) % 10}`;
} else if (i < 100000) {
f = `${Math.floor(i / 1000)}k`;
} else {
f = `Δms`;
}
const {stack} = new Error('ignore logger');
const stackStr = modifyStack(stack, relPath, filePath, false, true);
if (lastTime != null) {
const time = `On ${t.getFullYear()}-${_(1 + t.getMonth())}-${_(t.getDate())} at ${_(t.getHours())}:${_(t.getMinutes())}`;
if (time !== lastTime) {
logs.push(time);
stacks.push(stackStr);
if (listener) listener({message: time, stack: stackStr});
lastTime = time;
}
}
const message = `${s}:${ms}(${f}) ${a}`;
logs.push(message);
stacks.push(stackStr);
if (useConcole) {
console.log(message + "\n" + localStack(stackStr, stack));
}
if (listener) listener({message, stack: localStack(stackStr, stack)})
};
const join = errs => errs && Array.isArray(errs) && errs.map(e => e && e.stack && modifyStack(e.stack, relPath, filePath, true, false)).join('\n');
const logger = {
log: log.bind(this, "log"),
debug: log.bind(this, "debug"),
error: log.bind(this, "error"),
info: log.bind(this, "info"),
join,
clean: () => lastIndex = logs.length,
toString: () => logs.slice(lastIndex).join("\n"),
stacks: () => logs.slice(lastIndex).map((log, i)=> log+'\n'+stacks[lastIndex+i]).join("\n"),
all: () => logs.join("\n"),
allStacks: () => logs.map((log, i)=> log+'\n'+stacks[i]).join("\n"),
};
const transformException = e => createErr(e, relPath, filePath, logger);
logger.transformException = transformException;
return logger;
}
function createErr(e, relPath, filePath, logger) {
const {message} = e;
const stack = modifyStack("" + e.stack, relPath, filePath, true, false);
return {message, stack, toString: () => message, logger};
}
function modifyStack(stack, relPath, filePath, includeFirstLine, includeReference) {
const stackArr = [];
const arr = stack.split('\n');
let modulinoIncluded = false;
arr.forEach((line, index) => {
if (index === 0) {
if (includeFirstLine) {
stackArr.push(line);
}
return;
}
let where;
let lineStart;
let endStr;
const start = line.indexOf('(');
if (start > 0) {
lineStart = line.substr(0, start + 1);
const end = line.indexOf(')', start);
if (end > start) {
where = line.substring(start + 1, end);
endStr = line.substr(end);
}
} else {
const lastSpace = line.lastIndexOf(' ');
if (lastSpace > 2 && line.substr(lastSpace - 3, 4) === ' at ') {
lineStart = line.substr(0, lastSpace + 1)+'(';
where = line.substring(lastSpace + 1);
endStr = ')';
}
}
if (where) {
const modulino = where.indexOf('/node_modules/modulino/');
if (modulino >= 0) {
if (!modulinoIncluded) {
if (!stack.startsWith('Error: ignore logger\n') || !where.includes('/logger.js:')) {
modulinoIncluded = true;
stackArr.push(lineStart + where.substr(modulino) + endStr);
}
}
} else if (where.includes(relPath)) {
stackArr.push(lineStart + where + endStr);
}
}
});
if (includeReference && filePath) {
stackArr.push(' at global code (' + relPath + filePath + ':1:1)');
}
return stackArr.join('\n');
}
const dir = process.cwd();
const localStack = (str, stack) => str || (""+stack.split('\n')[2]).replace(dir, '.');
const rootLogger = createLogger('./', '');
let useConcole = false;
const logToConsole = bool => useConcole = bool;
module.exports = {createLogger, setListener, rootLogger, logToConsole};