fix(chat): downgrade media prefetch cache failures
This commit is contained in:
+75
-2
@@ -110,8 +110,17 @@ export class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
const serializableValue = Logger.toSerializableLogValue(value);
|
||||
if (serializableValue !== value) {
|
||||
try {
|
||||
return JSON.stringify(serializableValue, null, 2);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.stringify(value, null, 2);
|
||||
return JSON.stringify(serializableValue, null, 2);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
@@ -206,7 +215,10 @@ export class Logger {
|
||||
return;
|
||||
}
|
||||
|
||||
console[method](`${prefix} ${message}`, ...data);
|
||||
console[method](
|
||||
`${prefix} ${message}`,
|
||||
...Logger.toBrowserConsoleData(data),
|
||||
);
|
||||
}
|
||||
|
||||
private static toBrowserConsolePayload(args: LogArgs): {
|
||||
@@ -235,6 +247,67 @@ export class Logger {
|
||||
};
|
||||
}
|
||||
|
||||
private static toBrowserConsoleData(data: unknown[]): unknown[] {
|
||||
return data.map((item) => Logger.toSerializableLogValue(item));
|
||||
}
|
||||
|
||||
private static toSerializableLogValue(
|
||||
value: unknown,
|
||||
seen = new WeakSet<object>(),
|
||||
): unknown {
|
||||
if (value instanceof Error) {
|
||||
return Logger.serializeError(value, seen);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => Logger.toSerializableLogValue(item, seen));
|
||||
}
|
||||
|
||||
if (typeof value !== "object" || value === null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
|
||||
if (seen.has(value)) {
|
||||
return "[Circular]";
|
||||
}
|
||||
seen.add(value);
|
||||
|
||||
const output: Record<string, unknown> = {};
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
output[key] = Logger.toSerializableLogValue(item, seen);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private static serializeError(
|
||||
error: Error,
|
||||
seen: WeakSet<object>,
|
||||
): Record<string, unknown> {
|
||||
const output: Record<string, unknown> = {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
};
|
||||
|
||||
if (seen.has(error)) {
|
||||
return output;
|
||||
}
|
||||
seen.add(error);
|
||||
|
||||
for (const key of Object.getOwnPropertyNames(error)) {
|
||||
if (key in output) continue;
|
||||
output[key] = Logger.toSerializableLogValue(
|
||||
(error as unknown as Record<string, unknown>)[key],
|
||||
seen,
|
||||
);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/** 透传 raw pino logger(高级用法:自定义 binding / child / serializers) */
|
||||
get raw(): PinoLogger {
|
||||
return this.logger;
|
||||
|
||||
Reference in New Issue
Block a user