From 5b93c1d2f934a2bb3e12087e016e7fdec0c36512 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 27 Jan 2023 04:58:13 -0800 Subject: [PATCH] chore: truncate long protocol logs (#20412) This affects the logs in the `DEBUG=pw:protocol` mode so that they never span more then 10 lines of 80-character-width terminal. --- packages/playwright-core/src/server/helper.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/server/helper.ts b/packages/playwright-core/src/server/helper.ts index 51f1bf61d2..ab59987a40 100644 --- a/packages/playwright-core/src/server/helper.ts +++ b/packages/playwright-core/src/server/helper.ts @@ -22,6 +22,8 @@ import { debugLogger } from '../common/debugLogger'; import type { RegisteredListener } from '../utils/eventsHelper'; import { eventsHelper } from '../utils/eventsHelper'; +const MAX_LOG_LENGTH = process.env.MAX_LOG_LENGTH ? +process.env.MAX_LOG_LENGTH : Infinity; + class Helper { static completeUserURL(urlString: string): string { if (urlString.startsWith('localhost') || urlString.startsWith('127.0.0.1')) @@ -84,8 +86,12 @@ class Helper { return (direction: 'send' | 'receive', message: object) => { if (protocolLogger) protocolLogger(direction, message); - if (debugLogger.isEnabled('protocol')) - debugLogger.log('protocol', (direction === 'send' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(message)); + if (debugLogger.isEnabled('protocol')) { + let text = JSON.stringify(message); + if (text.length > MAX_LOG_LENGTH) + text = text.substring(0, MAX_LOG_LENGTH / 2) + ' <<<<<( LOG TRUNCATED )>>>>> ' + text.substring(text.length - MAX_LOG_LENGTH / 2); + debugLogger.log('protocol', (direction === 'send' ? 'SEND ► ' : '◀ RECV ') + text); + } }; }