Ver Fonte

登录页面上传本地加密日志

Michael Wang há 5 anos atrás
pai
commit
4cb33b7713
3 ficheiros alterados com 124 adições e 1 exclusões
  1. 8 1
      src/features/Login/Login.vue
  2. 112 0
      src/utils/logger.js
  3. 4 0
      src/utils/loghub-tracking.js

+ 8 - 1
src/features/Login/Login.vue

@@ -145,7 +145,11 @@ import {
   VUE_APP_CONFIG_FILE_SEVER_URL,
 } from "@/constants/constants";
 import UA, { chromeUA } from "@/utils/ua.js";
-import { createLog, createUserDetailLog } from "@/utils/logger";
+import {
+  createLog,
+  createUserDetailLog,
+  createEncryptLog,
+} from "@/utils/logger";
 import VerifyCode from "./VerifyCode.vue";
 import logNewVersion from "./HiddenRequest";
 
@@ -361,6 +365,8 @@ export default {
       }
       this.login();
     }
+    // 上传本机加密日志
+    this.uploadLogInterval = setInterval(() => createEncryptLog(), 5 * 1000);
   },
   async created() {
     createLog({
@@ -497,6 +503,7 @@ export default {
   },
   beforeDestroy() {
     clearTimeout(this.loginTimeout);
+    clearInterval(this.uploadLogInterval);
     // clearInterval(this.checkNewVersionInterval);
     document.removeEventListener(
       "__newSWAvailable",

+ 112 - 0
src/utils/logger.js

@@ -74,3 +74,115 @@ export function createLog(logs) {
     window._hmt.push(["_trackEvent", "创建用户日志出错-2"]);
   }
 }
+
+export function createEncryptLog() {
+  const isElectron = typeof nodeRequire != "undefined";
+  // 非 electron 返回
+  if (!isElectron) return;
+
+  try {
+    const user = store.state.user;
+    const uuidForEcs = localStorage.getItem("uuidForEcs");
+    if (uuidForEcs) {
+      logger.push("uuidForEcs", uuidForEcs);
+    } else {
+      // 没有 uuidForEcs 日志没法查询
+      return;
+    }
+    if (user) {
+      logger.push("userId", user.id);
+    }
+
+    let log = null;
+    try {
+      let lastLogIndex = (localStorage.getItem("lastLogIndex") || 0) - 0;
+      log = window.nodeRequire("electron-log");
+      // const filePath = log.getFile().path;
+      const filePath = log.transports.file.findLogPath();
+      const fs = window.nodeRequire("fs");
+      const content = fs.readFileSync(filePath, "utf-8");
+      const ary = content
+        .toString()
+        .split("\r\n")
+        .join("\n")
+        .split("\n");
+      // 重复上传没有时间的行:跨过非时间戳的行; 错误识别:全部重新执行
+      // let lastIndex = ary.findIndex(v => v === lastLog);
+      // console.log({ lastIndex });
+      if (ary.length < lastLogIndex) {
+        lastLogIndex = 0;
+      }
+
+      let logLen = 10;
+      const newAry = ary
+        .slice(lastLogIndex, lastLogIndex + logLen)
+        .filter(v => v);
+      // 如果没有上传的内容,则不修改lastLog, 也不上传
+      if (!newAry.length) return;
+      lastLogIndex = lastLogIndex + newAry.length;
+
+      localStorage.setItem("lastLogIndex", lastLogIndex);
+      logger.push("encryptLog", newAry.join("\n"));
+    } catch (error) {
+      console.debug(error);
+      return;
+    }
+
+    logger.push("clientDate", moment().format("hh:mm:ss.SSS"));
+    logger.logger();
+  } catch (error) {
+    console.log(error);
+    window._hmt.push(["_trackEvent", "创建用户日志出错-3"]);
+  }
+}
+
+// const postLog = body => {
+//   const headers = new Headers();
+//   headers.append("x-log-apiversion", "0.6.0");
+
+//   fetch(`https://${project}.${host}/logstores/${logstore}`, {
+//     method: "post",
+//     headers,
+//     body,
+//   });
+// };
+
+// export function createEncryptLog() {
+//   try {
+//     const user = store.state.user;
+//     const uuidForEcs = localStorage.getItem("uuidForEcs");
+//     const singleLog = {};
+//     const __logs__ = [singleLog];
+//     if (uuidForEcs) {
+//       singleLog.uuidForEcs = uuidForEcs;
+//     } else {
+//       // 没有 uuidForEcs 日志没法查询
+//       return;
+//     }
+//     if (user) {
+//       singleLog.userId = user.id;
+//     }
+//     const isElectron = typeof nodeRequire != "undefined";
+//     let log = null;
+//     if (isElectron) {
+//       try {
+//         log = window.nodeRequire("electron-log");
+//         // const filePath = log.getFile().path;
+//         const filePath = log.transports.file.findLogPath();
+//         const fs = window.nodeRequire("fs");
+//         const encryptLog = fs.readFileSync(filePath, "utf-8");
+//         singleLog.encryptLog = encryptLog;
+
+//         postLog(JSON.stringify({ __logs__ }));
+//       } catch (error) {
+//         console.debug(error);
+//         return;
+//       }
+//     }
+//     singleLog.clientDate = moment().format("hh:mm:ss.SSS");
+//     postLog(__logs__);
+//   } catch (error) {
+//     console.log(error);
+//     window._hmt.push(["_trackEvent", "创建用户日志出错-3"]);
+//   }
+// }

+ 4 - 0
src/utils/loghub-tracking.js

@@ -60,6 +60,10 @@
             "Failed to log to ali log service because of this exception:\n" + ex
           );
           console.log("Failed log data:", url);
+          // wzj-modified: 若上传失败,则重新上传本地日志
+          if (location.pathname.startsWith("/oe/login")) {
+            localStorage.setItem("lastLogIndex", 0);
+          }
         }
       }
     },