Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 18 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, '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, 'RECV');
this._dispatcherConnection.dispatch(jsonMessage);
});

ws.on('close', () => this._onDisconnect());
Expand Down Expand Up @@ -245,6 +250,18 @@ export class PlaywrightConnection {
debugLogger.log('server', `[${this._id}] finished cleanup`);
}

private logServerMetadata(message: object, direction: 'SEND' | 'RECV') {
const serverLogMetadata: { [key: string]: any } = {
timestamp: Date.now(),
Sid200026 marked this conversation as resolved.
Show resolved Hide resolved
payloadSizeInBytes: Buffer.byteLength(JSON.stringify(message), 'utf-8')
Sid200026 marked this conversation as resolved.
Show resolved Hide resolved
};
['id', 'guid', 'method'].forEach(key => {
Sid200026 marked this conversation as resolved.
Show resolved Hide resolved
if (key in message)
serverLogMetadata[key] = (message as any)[key];
});
debugLogger.log('server:metadata', (direction === 'SEND' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(serverLogMetadata));
}

async close(reason?: { code: number, reason: string }) {
if (this._disconnected)
return;
Expand Down