🪚 refactor: Optimize CONSOLE_JSON Debug Logs with Truncation (#4709)

This commit is contained in:
Danny Avila 2024-11-12 21:53:56 -05:00 committed by GitHub
parent 95201908e9
commit 493e64e6db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View file

@ -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 = { module.exports = {
redactFormat, redactFormat,
redactMessage, redactMessage,
debugTraverse, debugTraverse,
jsonTruncateFormat,
}; };

View file

@ -1,7 +1,7 @@
const path = require('path'); const path = require('path');
const winston = require('winston'); const winston = require('winston');
require('winston-daily-rotate-file'); require('winston-daily-rotate-file');
const { redactFormat, redactMessage, debugTraverse } = require('./parsers'); const { redactFormat, redactMessage, debugTraverse, jsonTruncateFormat } = require('./parsers');
const logDir = path.join(__dirname, '..', 'logs'); const logDir = path.join(__dirname, '..', 'logs');
@ -112,7 +112,7 @@ if (useDebugConsole) {
new winston.transports.Console({ new winston.transports.Console({
level: 'debug', level: 'debug',
format: useConsoleJson format: useConsoleJson
? winston.format.combine(fileFormat, debugTraverse, winston.format.json()) ? winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json())
: winston.format.combine(fileFormat, debugTraverse), : winston.format.combine(fileFormat, debugTraverse),
}), }),
); );
@ -120,7 +120,7 @@ if (useDebugConsole) {
transports.push( transports.push(
new winston.transports.Console({ new winston.transports.Console({
level: 'info', level: 'info',
format: winston.format.combine(fileFormat, winston.format.json()), format: winston.format.combine(fileFormat, jsonTruncateFormat(), winston.format.json()),
}), }),
); );
} else { } else {