Skip to content

Commit

Permalink
chore(logs): Add new log level to capture client-server message's met…
Browse files Browse the repository at this point in the history
…adata information (#28141)

Goal - Capture minimal diagnostic information for each message being
sent between the playwright client and server.

---------

Co-authored-by: Siddharth Singha Roy <ssingharoy@microsoft.com>
  • Loading branch information
Sid200026 and Siddharth Singha Roy authored Nov 15, 2023
1 parent 80bab8a commit 4575c9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/playwright-core/src/common/debugLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 17 additions & 1 deletion packages/playwright-core/src/remote/playwrightConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4575c9a

Please sign in to comment.