@docusaurus/logger 0.0.0-6106 → 0.0.0-6109
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/lib/perfLogger.d.ts.map +1 -1
- package/lib/perfLogger.js +40 -20
- package/lib/perfLogger.js.map +1 -1
- package/package.json +2 -2
- package/src/perfLogger.ts +48 -21
package/lib/perfLogger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"perfLogger.d.ts","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"perfLogger.d.ts","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":"AA8BA,KAAK,aAAa,GAAG;IACnB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,KAAK,EAAE,CAAC,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,KACpC,OAAO,CAAC,MAAM,CAAC,CAAC;CACtB,CAAC;AAuIF,eAAO,MAAM,UAAU,EAAE,aAAkC,CAAC"}
|
package/lib/perfLogger.js
CHANGED
|
@@ -12,13 +12,13 @@ const async_hooks_1 = require("async_hooks");
|
|
|
12
12
|
const logger_1 = tslib_1.__importDefault(require("./logger"));
|
|
13
13
|
// For now this is a private env variable we use internally
|
|
14
14
|
// But we'll want to expose this feature officially some day
|
|
15
|
-
const PerfDebuggingEnabled =
|
|
15
|
+
const PerfDebuggingEnabled = process.env.DOCUSAURUS_PERF_LOGGER === 'true';
|
|
16
16
|
const Thresholds = {
|
|
17
17
|
min: 5,
|
|
18
18
|
yellow: 100,
|
|
19
19
|
red: 1000,
|
|
20
20
|
};
|
|
21
|
-
const PerfPrefix = logger_1.default.yellow(`[PERF]
|
|
21
|
+
const PerfPrefix = logger_1.default.yellow(`[PERF]`);
|
|
22
22
|
// This is what enables to "see the parent stack" for each log
|
|
23
23
|
// Parent1 > Parent2 > Parent3 > child trace
|
|
24
24
|
const ParentPrefix = new async_hooks_1.AsyncLocalStorage();
|
|
@@ -26,6 +26,12 @@ function applyParentPrefix(label) {
|
|
|
26
26
|
const parentPrefix = ParentPrefix.getStore();
|
|
27
27
|
return parentPrefix ? `${parentPrefix} > ${label}` : label;
|
|
28
28
|
}
|
|
29
|
+
function getMemory() {
|
|
30
|
+
// Before reading memory stats, we explicitly call the GC
|
|
31
|
+
// Note: this only works when Node.js option "--expose-gc" is provided
|
|
32
|
+
globalThis.gc?.();
|
|
33
|
+
return process.memoryUsage();
|
|
34
|
+
}
|
|
29
35
|
function createPerfLogger() {
|
|
30
36
|
if (!PerfDebuggingEnabled) {
|
|
31
37
|
const noop = () => { };
|
|
@@ -51,15 +57,18 @@ function createPerfLogger() {
|
|
|
51
57
|
const fmtHead = (bytes) => logger_1.default.cyan(`${(bytes / 1000000).toFixed(0)}mb`);
|
|
52
58
|
return logger_1.default.dim(`(${fmtHead(memory.before.heapUsed)} -> ${fmtHead(memory.after.heapUsed)})`);
|
|
53
59
|
};
|
|
54
|
-
const
|
|
60
|
+
const formatStatus = (error) => {
|
|
61
|
+
return error ? logger_1.default.red('[KO]') : ''; // logger.green('[OK]');
|
|
62
|
+
};
|
|
63
|
+
const printPerfLog = ({ label, duration, memory, error, }) => {
|
|
55
64
|
if (duration < Thresholds.min) {
|
|
56
65
|
return;
|
|
57
66
|
}
|
|
58
|
-
console.log(`${PerfPrefix
|
|
67
|
+
console.log(`${PerfPrefix}${formatStatus(error)} ${label} - ${formatDuration(duration)} - ${formatMemory(memory)}`);
|
|
59
68
|
};
|
|
60
69
|
const start = (label) => performance.mark(label, {
|
|
61
70
|
detail: {
|
|
62
|
-
memoryUsage:
|
|
71
|
+
memoryUsage: getMemory(),
|
|
63
72
|
},
|
|
64
73
|
});
|
|
65
74
|
const end = (label) => {
|
|
@@ -70,27 +79,38 @@ function createPerfLogger() {
|
|
|
70
79
|
duration,
|
|
71
80
|
memory: {
|
|
72
81
|
before: memoryUsage,
|
|
73
|
-
after:
|
|
82
|
+
after: getMemory(),
|
|
74
83
|
},
|
|
84
|
+
error: undefined,
|
|
75
85
|
});
|
|
76
86
|
};
|
|
77
|
-
const log = (label) => console.log(PerfPrefix
|
|
87
|
+
const log = (label) => console.log(`${PerfPrefix} ${applyParentPrefix(label)}`);
|
|
78
88
|
const async = async (label, asyncFn) => {
|
|
79
89
|
const finalLabel = applyParentPrefix(label);
|
|
80
90
|
const before = performance.now();
|
|
81
|
-
const memoryBefore =
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
const memoryBefore = getMemory();
|
|
92
|
+
const asyncEnd = ({ error }) => {
|
|
93
|
+
const memoryAfter = getMemory();
|
|
94
|
+
const duration = performance.now() - before;
|
|
95
|
+
printPerfLog({
|
|
96
|
+
error,
|
|
97
|
+
label: finalLabel,
|
|
98
|
+
duration,
|
|
99
|
+
memory: {
|
|
100
|
+
before: memoryBefore,
|
|
101
|
+
after: memoryAfter,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
try {
|
|
106
|
+
const result = await ParentPrefix.run(finalLabel, () => asyncFn());
|
|
107
|
+
asyncEnd({ error: undefined });
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
asyncEnd({ error: e });
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
94
114
|
};
|
|
95
115
|
return {
|
|
96
116
|
start,
|
package/lib/perfLogger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"perfLogger.js","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":";;;;AAAA;;;;;GAKG;AACH,6CAA8C;AAC9C,8DAA8B;AAE9B,2DAA2D;AAC3D,4DAA4D;AAC5D,MAAM,oBAAoB,
|
|
1
|
+
{"version":3,"file":"perfLogger.js","sourceRoot":"","sources":["../src/perfLogger.ts"],"names":[],"mappings":";;;;AAAA;;;;;GAKG;AACH,6CAA8C;AAC9C,8DAA8B;AAE9B,2DAA2D;AAC3D,4DAA4D;AAC5D,MAAM,oBAAoB,GACxB,OAAO,CAAC,GAAG,CAAC,sBAAsB,KAAK,MAAM,CAAC;AAEhD,MAAM,UAAU,GAAG;IACjB,GAAG,EAAE,CAAC;IACN,MAAM,EAAE,GAAG;IACX,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,UAAU,GAAG,gBAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAE3C,8DAA8D;AAC9D,4CAA4C;AAC5C,MAAM,YAAY,GAAG,IAAI,+BAAiB,EAAU,CAAC;AACrD,SAAS,iBAAiB,CAAC,KAAa;IACtC,MAAM,YAAY,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC;IAC7C,OAAO,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7D,CAAC;AAiBD,SAAS,SAAS;IAChB,yDAAyD;IACzD,sEAAsE;IACtE,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC;IAElB,OAAO,OAAO,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC;AAED,SAAS,gBAAgB;IACvB,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;QACtB,OAAO;YACL,KAAK,EAAE,IAAI;YACX,GAAG,EAAE,IAAI;YACT,GAAG,EAAE,IAAI;YACT,KAAK,EAAE,KAAK,EAAE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;QACjC,MAAM,YAAY,GAAG,SAAS,EAAE,CAAC;QAEjC,MAAM,QAAQ,GAAG,CAAC,EAAC,KAAK,EAA6B,EAAE,EAAE;YACvD,MAAM,WAAW,GAAG,SAAS,EAAE,CAAC;YAChC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC;YAC5C,YAAY,CAAC;gBACX,KAAK;gBACL,KAAK,EAAE,UAAU;gBACjB,QAAQ;gBACR,MAAM,EAAE;oBACN,MAAM,EAAE,YAAY;oBACpB,KAAK,EAAE,WAAW;iBACnB;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;YACnE,QAAQ,CAAC,EAAC,KAAK,EAAE,SAAS,EAAC,CAAC,CAAC;YAC7B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,QAAQ,CAAC,EAAC,KAAK,EAAE,CAAU,EAAC,CAAC,CAAC;YAC9B,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,KAAK;QACL,GAAG;QACH,GAAG;QACH,KAAK;KACN,CAAC;AACJ,CAAC;AAEY,QAAA,UAAU,GAAkB,gBAAgB,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/logger",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-6109",
|
|
4
4
|
"description": "An encapsulated logger for semantically formatting console messages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/supports-color": "^8.1.1"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "d1f5cd6af4cb380de04e370bdf94781615cecd58"
|
|
33
33
|
}
|
package/src/perfLogger.ts
CHANGED
|
@@ -9,7 +9,8 @@ import logger from './logger';
|
|
|
9
9
|
|
|
10
10
|
// For now this is a private env variable we use internally
|
|
11
11
|
// But we'll want to expose this feature officially some day
|
|
12
|
-
const PerfDebuggingEnabled: boolean =
|
|
12
|
+
const PerfDebuggingEnabled: boolean =
|
|
13
|
+
process.env.DOCUSAURUS_PERF_LOGGER === 'true';
|
|
13
14
|
|
|
14
15
|
const Thresholds = {
|
|
15
16
|
min: 5,
|
|
@@ -17,7 +18,7 @@ const Thresholds = {
|
|
|
17
18
|
red: 1000,
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
const PerfPrefix = logger.yellow(`[PERF]
|
|
21
|
+
const PerfPrefix = logger.yellow(`[PERF]`);
|
|
21
22
|
|
|
22
23
|
// This is what enables to "see the parent stack" for each log
|
|
23
24
|
// Parent1 > Parent2 > Parent3 > child trace
|
|
@@ -42,6 +43,14 @@ type Memory = {
|
|
|
42
43
|
after: NodeJS.MemoryUsage;
|
|
43
44
|
};
|
|
44
45
|
|
|
46
|
+
function getMemory(): NodeJS.MemoryUsage {
|
|
47
|
+
// Before reading memory stats, we explicitly call the GC
|
|
48
|
+
// Note: this only works when Node.js option "--expose-gc" is provided
|
|
49
|
+
globalThis.gc?.();
|
|
50
|
+
|
|
51
|
+
return process.memoryUsage();
|
|
52
|
+
}
|
|
53
|
+
|
|
45
54
|
function createPerfLogger(): PerfLoggerAPI {
|
|
46
55
|
if (!PerfDebuggingEnabled) {
|
|
47
56
|
const noop = () => {};
|
|
@@ -73,29 +82,35 @@ function createPerfLogger(): PerfLoggerAPI {
|
|
|
73
82
|
);
|
|
74
83
|
};
|
|
75
84
|
|
|
85
|
+
const formatStatus = (error: Error | undefined): string => {
|
|
86
|
+
return error ? logger.red('[KO]') : ''; // logger.green('[OK]');
|
|
87
|
+
};
|
|
88
|
+
|
|
76
89
|
const printPerfLog = ({
|
|
77
90
|
label,
|
|
78
91
|
duration,
|
|
79
92
|
memory,
|
|
93
|
+
error,
|
|
80
94
|
}: {
|
|
81
95
|
label: string;
|
|
82
96
|
duration: number;
|
|
83
97
|
memory: Memory;
|
|
98
|
+
error: Error | undefined;
|
|
84
99
|
}) => {
|
|
85
100
|
if (duration < Thresholds.min) {
|
|
86
101
|
return;
|
|
87
102
|
}
|
|
88
103
|
console.log(
|
|
89
|
-
`${PerfPrefix
|
|
90
|
-
|
|
91
|
-
)}`,
|
|
104
|
+
`${PerfPrefix}${formatStatus(error)} ${label} - ${formatDuration(
|
|
105
|
+
duration,
|
|
106
|
+
)} - ${formatMemory(memory)}`,
|
|
92
107
|
);
|
|
93
108
|
};
|
|
94
109
|
|
|
95
110
|
const start: PerfLoggerAPI['start'] = (label) =>
|
|
96
111
|
performance.mark(label, {
|
|
97
112
|
detail: {
|
|
98
|
-
memoryUsage:
|
|
113
|
+
memoryUsage: getMemory(),
|
|
99
114
|
},
|
|
100
115
|
});
|
|
101
116
|
|
|
@@ -110,30 +125,42 @@ function createPerfLogger(): PerfLoggerAPI {
|
|
|
110
125
|
duration,
|
|
111
126
|
memory: {
|
|
112
127
|
before: memoryUsage,
|
|
113
|
-
after:
|
|
128
|
+
after: getMemory(),
|
|
114
129
|
},
|
|
130
|
+
error: undefined,
|
|
115
131
|
});
|
|
116
132
|
};
|
|
117
133
|
|
|
118
134
|
const log: PerfLoggerAPI['log'] = (label: string) =>
|
|
119
|
-
console.log(PerfPrefix
|
|
135
|
+
console.log(`${PerfPrefix} ${applyParentPrefix(label)}`);
|
|
120
136
|
|
|
121
137
|
const async: PerfLoggerAPI['async'] = async (label, asyncFn) => {
|
|
122
138
|
const finalLabel = applyParentPrefix(label);
|
|
123
139
|
const before = performance.now();
|
|
124
|
-
const memoryBefore =
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
140
|
+
const memoryBefore = getMemory();
|
|
141
|
+
|
|
142
|
+
const asyncEnd = ({error}: {error: Error | undefined}) => {
|
|
143
|
+
const memoryAfter = getMemory();
|
|
144
|
+
const duration = performance.now() - before;
|
|
145
|
+
printPerfLog({
|
|
146
|
+
error,
|
|
147
|
+
label: finalLabel,
|
|
148
|
+
duration,
|
|
149
|
+
memory: {
|
|
150
|
+
before: memoryBefore,
|
|
151
|
+
after: memoryAfter,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
const result = await ParentPrefix.run(finalLabel, () => asyncFn());
|
|
158
|
+
asyncEnd({error: undefined});
|
|
159
|
+
return result;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
asyncEnd({error: e as Error});
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
137
164
|
};
|
|
138
165
|
|
|
139
166
|
return {
|