mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00
⭕ fix: Handle Circular References in CONSOLE_JSON
Log Truncation (#4958)
This commit is contained in:
parent
69bd8e3644
commit
43d10a4e43
1 changed files with 21 additions and 5 deletions
|
@ -187,17 +187,33 @@ const debugTraverse = winston.format.printf(({ level, message, timestamp, ...met
|
||||||
});
|
});
|
||||||
|
|
||||||
const jsonTruncateFormat = winston.format((info) => {
|
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) => {
|
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 = {};
|
const newObj = {};
|
||||||
Object.entries(obj).forEach(([key, value]) => {
|
Object.entries(obj).forEach(([key, value]) => {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
newObj[key] = truncateLongStrings(value, 255);
|
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 {
|
} else {
|
||||||
newObj[key] = value;
|
newObj[key] = truncateObject(value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return newObj;
|
return newObj;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue