Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(logs): Add new log level to capture client-server message's metadata information (#28141)

Siddharth Singha Roy4575c9a182b7
diff --git a/packages/playwright-core/src/common/debugLogger.ts b/packages/playwright-core/src/common/debugLogger.ts
index 6948058685..bda373e0ef 100644
--- a/packages/playwright-core/src/common/debugLogger.ts
+++ b/packages/playwright-core/src/common/debugLogger.ts
@@ -28,6 +28,7 @@ const debugLoggerColorMap = {
'channel': 33, // blue
'server': 45, // cyan
'server:channel': 34, // green
+ 'server:metadata': 33, // blue
};
export type LogName = keyof typeof debugLoggerColorMap;
diff --git a/packages/playwright-core/src/remote/playwrightConnection.ts b/packages/playwright-core/src/remote/playwrightConnection.ts
index 7da1eefee2..c6898f7148 100644
--- a/packages/playwright-core/src/remote/playwrightConnection.ts
+++ b/packages/playwright-core/src/remote/playwrightConnection.ts
@@ -76,15 +76,20 @@ export class PlaywrightConnection {
const messageString = JSON.stringify(message);
if (debugLogger.isEnabled('server:channel'))
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} SEND ► ${messageString}`);
+ if (debugLogger.isEnabled('server:metadata'))
+ this.logServerMetadata(message, messageString, 'SEND');
ws.send(messageString);
}
};
ws.on('message', async (message: string) => {
await lock;
const messageString = Buffer.from(message).toString();
+ const jsonMessage = JSON.parse(messageString);
if (debugLogger.isEnabled('server:channel'))
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} ◀ RECV ${messageString}`);
- this._dispatcherConnection.dispatch(JSON.parse(messageString));
+ if (debugLogger.isEnabled('server:metadata'))
+ this.logServerMetadata(jsonMessage, messageString, 'RECV');
+ this._dispatcherConnection.dispatch(jsonMessage);
});
ws.on('close', () => this._onDisconnect());
@@ -245,6 +250,17 @@ export class PlaywrightConnection {
debugLogger.log('server', `[${this._id}] finished cleanup`);
}
+ private logServerMetadata(message: object, messageString: string, direction: 'SEND' | 'RECV') {
+ const serverLogMetadata = {
+ wallTime: Date.now(),
+ id: (message as any).id,
+ guid: (message as any).guid,
+ method: (message as any).method,
+ payloadSizeInBytes: Buffer.byteLength(messageString, 'utf-8')
+ };
+ debugLogger.log('server:metadata', (direction === 'SEND' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(serverLogMetadata));
+ }
+
async close(reason?: { code: number, reason: string }) {
if (this._disconnected)
return;