fix: Handle Circular References in CONSOLE_JSON Log Truncation (#4958)

This commit is contained in:
Andrés Restrepo 2024-12-12 13:02:44 -05:00 committed by GitHub
parent 69bd8e3644
commit 43d10a4e43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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