Skip to content

Commit 4575c9a

Browse files
authored
chore(logs): Add new log level to capture client-server message's metadata information (#28141)
Goal - Capture minimal diagnostic information for each message being sent between the playwright client and server. --------- Co-authored-by: Siddharth Singha Roy <[email protected]>
1 parent 80bab8a commit 4575c9a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/playwright-core/src/common/debugLogger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const debugLoggerColorMap = {
2828
'channel': 33, // blue
2929
'server': 45, // cyan
3030
'server:channel': 34, // green
31+
'server:metadata': 33, // blue
3132
};
3233
export type LogName = keyof typeof debugLoggerColorMap;
3334

packages/playwright-core/src/remote/playwrightConnection.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,20 @@ export class PlaywrightConnection {
7676
const messageString = JSON.stringify(message);
7777
if (debugLogger.isEnabled('server:channel'))
7878
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} SEND ► ${messageString}`);
79+
if (debugLogger.isEnabled('server:metadata'))
80+
this.logServerMetadata(message, messageString, 'SEND');
7981
ws.send(messageString);
8082
}
8183
};
8284
ws.on('message', async (message: string) => {
8385
await lock;
8486
const messageString = Buffer.from(message).toString();
87+
const jsonMessage = JSON.parse(messageString);
8588
if (debugLogger.isEnabled('server:channel'))
8689
debugLogger.log('server:channel', `[${this._id}] ${monotonicTime() * 1000} ◀ RECV ${messageString}`);
87-
this._dispatcherConnection.dispatch(JSON.parse(messageString));
90+
if (debugLogger.isEnabled('server:metadata'))
91+
this.logServerMetadata(jsonMessage, messageString, 'RECV');
92+
this._dispatcherConnection.dispatch(jsonMessage);
8893
});
8994

9095
ws.on('close', () => this._onDisconnect());
@@ -245,6 +250,17 @@ export class PlaywrightConnection {
245250
debugLogger.log('server', `[${this._id}] finished cleanup`);
246251
}
247252

253+
private logServerMetadata(message: object, messageString: string, direction: 'SEND' | 'RECV') {
254+
const serverLogMetadata = {
255+
wallTime: Date.now(),
256+
id: (message as any).id,
257+
guid: (message as any).guid,
258+
method: (message as any).method,
259+
payloadSizeInBytes: Buffer.byteLength(messageString, 'utf-8')
260+
};
261+
debugLogger.log('server:metadata', (direction === 'SEND' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(serverLogMetadata));
262+
}
263+
248264
async close(reason?: { code: number, reason: string }) {
249265
if (this._disconnected)
250266
return;

0 commit comments

Comments
 (0)