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.
This commit is contained in:
Andrey Lushnikov 2023-01-27 04:58:13 -08:00 committed by GitHub
parent cf0ca2e662
commit 5b93c1d2f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
}
};
}