@percy/logger 1.30.0 → 1.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +7 -0
- package/dist/timing.js +64 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Logger from './logger.js';
|
|
2
|
+
import TimeIt from './timing.js';
|
|
2
3
|
export function logger(name) {
|
|
3
4
|
return new Logger().group(name);
|
|
4
5
|
}
|
|
@@ -23,6 +24,12 @@ Object.defineProperties(logger, {
|
|
|
23
24
|
},
|
|
24
25
|
loglevel: {
|
|
25
26
|
value: (...args) => logger.instance.loglevel(...args)
|
|
27
|
+
},
|
|
28
|
+
timeit: {
|
|
29
|
+
get: () => new TimeIt(logger.instance.group('timer'))
|
|
30
|
+
},
|
|
31
|
+
measure: {
|
|
32
|
+
value: (...args) => logger.timeit.measure(...args)
|
|
26
33
|
}
|
|
27
34
|
});
|
|
28
35
|
export default logger;
|
package/dist/timing.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export default class TimeIt {
|
|
2
|
+
// returns a singleton instance
|
|
3
|
+
constructor(log) {
|
|
4
|
+
let {
|
|
5
|
+
instance = this
|
|
6
|
+
} = this.constructor;
|
|
7
|
+
instance.log = log;
|
|
8
|
+
this.constructor.instance = instance;
|
|
9
|
+
return instance;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// this function has some code repeatition as it needs to handle both sync and async
|
|
13
|
+
// callbacks. It handles both cases when function is marked async but there is no await
|
|
14
|
+
// as well as functions which have async await and sync functions - including sync
|
|
15
|
+
// functions which returns a promise instead.
|
|
16
|
+
// So we need to check if callback is returning promise or not and handle accordingly
|
|
17
|
+
// it always returns exact same value/promise as callback function and measures time
|
|
18
|
+
// correctly.
|
|
19
|
+
measure(name, identifier, meta, callback) {
|
|
20
|
+
const startTime = Date.now();
|
|
21
|
+
let errorMsg;
|
|
22
|
+
let errorStack;
|
|
23
|
+
const logtime = () => {
|
|
24
|
+
const duration = Date.now() - startTime;
|
|
25
|
+
this.log.debug(`${name} - ${identifier} - ${duration / 1000}s`, {
|
|
26
|
+
durationMs: duration,
|
|
27
|
+
errorMsg,
|
|
28
|
+
errorStack,
|
|
29
|
+
...meta
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
let wasPromise = false;
|
|
33
|
+
try {
|
|
34
|
+
let ret = callback();
|
|
35
|
+
// if it returned a promise then we need to return a promise
|
|
36
|
+
if (ret.then !== undefined && ret.then != null) {
|
|
37
|
+
wasPromise = true;
|
|
38
|
+
return ret.catch(e => {
|
|
39
|
+
return {
|
|
40
|
+
error: e,
|
|
41
|
+
errorReturnedFromMeasure: true
|
|
42
|
+
};
|
|
43
|
+
}).then(result => {
|
|
44
|
+
if (result !== null && result !== void 0 && result.errorReturnedFromMeasure) {
|
|
45
|
+
errorMsg = result.error.message;
|
|
46
|
+
errorStack = result.error.stack;
|
|
47
|
+
}
|
|
48
|
+
logtime();
|
|
49
|
+
if (result !== null && result !== void 0 && result.errorReturnedFromMeasure) {
|
|
50
|
+
throw result.error;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return ret;
|
|
56
|
+
} catch (e) {
|
|
57
|
+
errorMsg = e.message;
|
|
58
|
+
errorStack = e.stack;
|
|
59
|
+
throw e;
|
|
60
|
+
} finally {
|
|
61
|
+
if (!wasPromise) logtime();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@percy/logger",
|
|
3
|
-
"version": "1.30.
|
|
3
|
+
"version": "1.30.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"test": "node ../../scripts/test",
|
|
33
33
|
"test:coverage": "yarn test --coverage"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "2f8fc42f57c3989de0c5ceca207854d8f66272d3"
|
|
36
36
|
}
|