|
@@ -6,6 +6,11 @@ const isElectron = () =>
|
|
|
// 检测是否在 Cypress 测试环境中
|
|
// 检测是否在 Cypress 测试环境中
|
|
|
const isTestEnvironment = () =>
|
|
const isTestEnvironment = () =>
|
|
|
typeof window !== 'undefined' && window.Cypress;
|
|
typeof window !== 'undefined' && window.Cypress;
|
|
|
|
|
+
|
|
|
|
|
+// 检测是否在浏览器环境中
|
|
|
|
|
+const isBrowser = () =>
|
|
|
|
|
+ typeof window !== 'undefined' && !isElectron() && !isTestEnvironment();
|
|
|
|
|
+
|
|
|
//避免obj是递归引用导致崩溃
|
|
//避免obj是递归引用导致崩溃
|
|
|
function safeStringify(obj) {
|
|
function safeStringify(obj) {
|
|
|
const cache = new Set();
|
|
const cache = new Set();
|
|
@@ -23,13 +28,20 @@ function proxyLog(level) {
|
|
|
const original = console[level];
|
|
const original = console[level];
|
|
|
return (...args) => {
|
|
return (...args) => {
|
|
|
const msg = args.map(v => typeof v === 'object' ? safeStringify(v) : String(v)).join(' ');
|
|
const msg = args.map(v => typeof v === 'object' ? safeStringify(v) : String(v)).join(' ');
|
|
|
- original(...args); // ① 控制台始终打印
|
|
|
|
|
-
|
|
|
|
|
|
|
+ let finalMsg = msg;
|
|
|
|
|
+ if (isBrowser()) {
|
|
|
|
|
+ const timestamp = new Date().toISOString();
|
|
|
|
|
+ finalMsg = `[${timestamp}] [${level}] ${msg}`;
|
|
|
|
|
+ original(finalMsg); // ① 浏览器环境:控制台打印带时间戳的格式化消息
|
|
|
|
|
+ } else {
|
|
|
|
|
+ original(...args); // ① Electron/其他环境:控制台打印原始参数
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 如果在测试环境中,直接返回,不发送任何网络请求
|
|
// 如果在测试环境中,直接返回,不发送任何网络请求
|
|
|
if (isTestEnvironment()) {
|
|
if (isTestEnvironment()) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (isElectron()) { // ② Electron 环境
|
|
if (isElectron()) { // ② Electron 环境
|
|
|
window.electronAPI.writeLog(level, msg).catch(() => {});
|
|
window.electronAPI.writeLog(level, msg).catch(() => {});
|
|
|
} else if (typeof fetch !== 'undefined') {
|
|
} else if (typeof fetch !== 'undefined') {
|