Skip to content

Commit 3db05b4

Browse files
committed
feat(log): log only user api calls with DEBUG=pw:api
1 parent 1f8508d commit 3db05b4

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const { WebKit } = require('./lib/server/webkit');
2525

2626
for (const className in api) {
2727
if (typeof api[className] === 'function')
28-
helper.installApiHooks(className, api[className]);
28+
helper.installApiHooks(className[0].toLowerCase() + className.substring(1), api[className]);
2929
}
3030

3131
module.exports = {

src/helper.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,28 @@ class Helper {
5151
if (!isAsync && !log.enabled)
5252
continue;
5353
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
54+
const syncStack: any = {};
55+
Error.captureStackTrace(syncStack);
5456
if (log.enabled) {
55-
if (args.length)
56-
log(`${className}.${methodName} %o`, args);
57-
else
58-
log(`${className}.${methodName}`);
57+
const frames = syncStack.stack.substring('Error\n'.length)
58+
.split('\n')
59+
.map((f: string) => f.replace(/\s+at\s/, '').trim());
60+
const userCall = frames.length <= 1 || !frames[1].includes('playwright/lib');
61+
if (userCall) {
62+
const match = /([^/\\]+)(:\d+:\d+)[)]?$/.exec(frames[1]);
63+
let location = '';
64+
if (match) {
65+
const fileName = helper.trimMiddle(match[1], 20 - match[2].length);
66+
location = `\u001b[33m[${fileName}${match[2]}]\u001b[39m `;
67+
}
68+
if (args.length)
69+
log(`${location}${className}.${methodName} %o`, args);
70+
else
71+
log(`${location}${className}.${methodName}`);
72+
}
5973
}
6074
if (!isAsync)
6175
return method.call(this, ...args);
62-
const syncStack: any = {};
63-
Error.captureStackTrace(syncStack);
6476
return method.call(this, ...args).catch((e: any) => {
6577
const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
6678
const clientStack = stack.substring(stack.indexOf('\n'));
@@ -225,6 +237,15 @@ class Helper {
225237
urlString = 'http://' + urlString;
226238
return urlString;
227239
}
240+
241+
static trimMiddle(string: string, maxLength: number) {
242+
if (string.length <= maxLength)
243+
return string;
244+
245+
const leftHalf = maxLength >> 1;
246+
const rightHalf = maxLength - leftHalf - 1;
247+
return string.substr(0, leftHalf) + '\u2026' + string.substr(this.length - rightHalf, rightHalf);
248+
}
228249
}
229250

230251
export function assert(value: any, message?: string): asserts value {

0 commit comments

Comments
 (0)