From 493e64e6dbe0c362a51415486806ae8483cd653e Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 12 Nov 2024 21:53:56 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=9A=20refactor:=20Optimize=20`CONSOLE?= =?UTF-8?q?=5FJSON`=20Debug=20Logs=20with=20Truncation=20(#4709)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/config/parsers.js | 21 +++++++++++++++++++++ api/config/winston.js | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/api/config/parsers.js b/api/config/parsers.js index 9de54c2ae9..7cedd014cb 100644 --- a/api/config/parsers.js +++ b/api/config/parsers.js @@ -186,8 +186,29 @@ const debugTraverse = winston.format.printf(({ level, message, timestamp, ...met } }); +const jsonTruncateFormat = winston.format((info) => { + const truncateObject = (obj) => { + 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; + } + }); + return newObj; + }; + + return truncateObject(info); +}); + module.exports = { redactFormat, redactMessage, debugTraverse, + jsonTruncateFormat, }; diff --git a/api/config/winston.js b/api/config/winston.js index 81e972fbbc..8f51b9963c 100644 --- a/api/config/winston.js +++ b/api/config/winston.js @@ -1,7 +1,7 @@ const path = require('path'); const winston = require('winston'); require('winston-daily-rotate-file'); -const { redactFormat, redactMessage, debugTraverse } = require('./parsers'); +const { redactFormat, redactMessage, debugTraverse, jsonTruncateFormat } = require('./parsers'); const logDir = path.join(__dirname, '..', 'logs'); @@ -112,7 +112,7 @@ if (useDebugConsole) { new winston.transports.Console({ level: 'debug', format: useConsoleJson - ? winston.format.combine(fileFormat, debugTraverse, winston.format.json()) + ? winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json()) : winston.format.combine(fileFormat, debugTraverse), }), ); @@ -120,7 +120,7 @@ if (useDebugConsole) { transports.push( new winston.transports.Console({ level: 'info', - format: winston.format.combine(fileFormat, winston.format.json()), + format: winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json()), }), ); } else {