From 43d10a4e43057d253bb3f9a9cc8fb04b53ec0320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Restrepo?= Date: Thu, 12 Dec 2024 13:02:44 -0500 Subject: [PATCH] =?UTF-8?q?=E2=AD=95=20fix:=20Handle=20Circular=20Referenc?= =?UTF-8?q?es=20in=20`CONSOLE=5FJSON`=20Log=20Truncation=20(#4958)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/config/parsers.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/api/config/parsers.js b/api/config/parsers.js index 7cedd014c..a3bab7d3c 100644 --- a/api/config/parsers.js +++ b/api/config/parsers.js @@ -187,17 +187,33 @@ const debugTraverse = winston.format.printf(({ level, message, timestamp, ...met }); const jsonTruncateFormat = winston.format((info) => { + const truncateLongStrings = (str, maxLength) => { + return str.length > maxLength ? str.substring(0, maxLength) + '...' : str; + }; + + const seen = new WeakSet(); + const truncateObject = (obj) => { + if (typeof obj !== 'object' || obj === null) { + return obj; + } + + // Handle circular references + if (seen.has(obj)) { + return '[Circular]'; + } + seen.add(obj); + + if (Array.isArray(obj)) { + return obj.map(item => truncateObject(item)); + } + const newObj = {}; Object.entries(obj).forEach(([key, value]) => { if (typeof value === 'string') { newObj[key] = truncateLongStrings(value, 255); - } else if (Array.isArray(value)) { - newObj[key] = value.map(condenseArray); - } else if (typeof value === 'object' && value !== null) { - newObj[key] = truncateObject(value); } else { - newObj[key] = value; + newObj[key] = truncateObject(value); } }); return newObj;