Skip to content

Commit

Permalink
feat(log): log only user api calls with DEBUG=pw:api
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Feb 15, 2020
1 parent 8cfdeb9 commit b853adc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { WebKit } = require('./lib/server/webkit');

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

module.exports = {
Expand Down
20 changes: 14 additions & 6 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,24 @@ class Helper {
if (!isAsync && !log.enabled)
continue;
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
const syncStack: any = {};
Error.captureStackTrace(syncStack);
if (log.enabled) {
if (args.length)
log(`${className}.${methodName} %o`, args);
else
log(`${className}.${methodName}`);
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]);
const location = match ? `\u001b[33m[${match[1]}]\u001b[39m ` : '';
if (args.length)
log(`${location}${className}.${methodName} %o`, args);
else
log(`${location}${className}.${methodName}`);
}
}
if (!isAsync)
return method.call(this, ...args);
const syncStack: any = {};
Error.captureStackTrace(syncStack);
return method.call(this, ...args).catch((e: any) => {
const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
const clientStack = stack.substring(stack.indexOf('\n'));
Expand Down

0 comments on commit b853adc

Please sign in to comment.