mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
feat(log): log only user api calls with DEBUG=pw:api (#1029)
This commit is contained in:
parent
d97ea70804
commit
ebcaadedf1
2
index.js
2
index.js
@ -25,7 +25,7 @@ const { WebKit } = require('./lib/server/webkit');
|
|||||||
|
|
||||||
for (const className in api) {
|
for (const className in api) {
|
||||||
if (typeof api[className] === 'function')
|
if (typeof api[className] === 'function')
|
||||||
helper.installApiHooks(className, api[className]);
|
helper.installApiHooks(className[0].toLowerCase() + className.substring(1), api[className]);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -51,16 +51,28 @@ class Helper {
|
|||||||
if (!isAsync && !log.enabled)
|
if (!isAsync && !log.enabled)
|
||||||
continue;
|
continue;
|
||||||
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
|
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
|
||||||
|
const syncStack: any = {};
|
||||||
|
Error.captureStackTrace(syncStack);
|
||||||
if (log.enabled) {
|
if (log.enabled) {
|
||||||
|
const frames = syncStack.stack.substring('Error\n'.length)
|
||||||
|
.split('\n')
|
||||||
|
.map((f: string) => f.replace(/\s+at\s/, '').trim());
|
||||||
|
const userCall = frames.length <= 1 || !frames[1].includes('playwright/lib');
|
||||||
|
if (userCall) {
|
||||||
|
const match = /([^/\\]+)(:\d+:\d+)[)]?$/.exec(frames[1]);
|
||||||
|
let location = '';
|
||||||
|
if (match) {
|
||||||
|
const fileName = helper.trimMiddle(match[1], 20 - match[2].length);
|
||||||
|
location = `\u001b[33m[${fileName}${match[2]}]\u001b[39m `;
|
||||||
|
}
|
||||||
if (args.length)
|
if (args.length)
|
||||||
log(`${className}.${methodName} %o`, args);
|
log(`${location}${className}.${methodName} %o`, args);
|
||||||
else
|
else
|
||||||
log(`${className}.${methodName}`);
|
log(`${location}${className}.${methodName}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!isAsync)
|
if (!isAsync)
|
||||||
return method.call(this, ...args);
|
return method.call(this, ...args);
|
||||||
const syncStack: any = {};
|
|
||||||
Error.captureStackTrace(syncStack);
|
|
||||||
return method.call(this, ...args).catch((e: any) => {
|
return method.call(this, ...args).catch((e: any) => {
|
||||||
const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
|
const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
|
||||||
const clientStack = stack.substring(stack.indexOf('\n'));
|
const clientStack = stack.substring(stack.indexOf('\n'));
|
||||||
@ -229,6 +241,15 @@ class Helper {
|
|||||||
urlString = 'http://' + urlString;
|
urlString = 'http://' + urlString;
|
||||||
return urlString;
|
return urlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static trimMiddle(string: string, maxLength: number) {
|
||||||
|
if (string.length <= maxLength)
|
||||||
|
return string;
|
||||||
|
|
||||||
|
const leftHalf = maxLength >> 1;
|
||||||
|
const rightHalf = maxLength - leftHalf - 1;
|
||||||
|
return string.substr(0, leftHalf) + '\u2026' + string.substr(this.length - rightHalf, rightHalf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assert(value: any, message?: string): asserts value {
|
export function assert(value: any, message?: string): asserts value {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user