From b853adce6327aea423f1f371638822800fa627d0 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 14 Feb 2020 21:29:58 -0800 Subject: [PATCH] feat(log): log only user api calls with DEBUG=pw:api --- index.js | 2 +- src/helper.ts | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4137eff9e11a31..7ab40e101897e9 100644 --- a/index.js +++ b/index.js @@ -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 = { diff --git a/src/helper.ts b/src/helper.ts index a250ed680b4811..350be1c8b87177 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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'));