-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (39 loc) · 987 Bytes
/
index.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
/**
*
* @param {*} obj
* @param {OptionsLogger} options
* @return {*}
*/
function proxyLogger(obj, options = {}) {
const regexExclude = options.regexExclude ?? false;
const loopStart = options.loopStart ?? 'Start'
const pushLog = options.logMethod ?? 'pushLog'
return new Proxy(obj, {
lastProp: loopStart,
get (target, prop) {
const self = this;
const origMethod = target[prop];
if (typeof origMethod !== 'function') {
return target[prop];
}
return function(...args) {
let time = process.hrtime();
let result = origMethod.apply(this, args);
let end = (process.hrtime(time)[1] / 1e6).toFixed(3);
if (!regexExclude || !regexExclude.test(prop)) {
const text = self.lastProp + ' --> | ' + end + ' ms | ' + prop;
target[pushLog](text, {}, 'graph');
self.lastProp = prop;
}
return result;
};
},
set(target, prop, value) {
target[prop] = value;
return true;
},
});
}
module.exports = {
proxyLogger
}