logger.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import "@/utils/loghub-tracking.js";
  2. import { VUE_APP_SLS_STORE_NAME } from "@/constants/constants";
  3. import moment from "moment";
  4. const host = "cn-shenzhen.log.aliyuncs.com";
  5. const project = "examcloud";
  6. const logstore = VUE_APP_SLS_STORE_NAME;
  7. const logger = new window.Tracker(`${host}`, `${project}`, `${logstore}`);
  8. // logger.push("customer", "zhangsan");
  9. // logger.push("product", "iphone 6s");
  10. // logger.push("price", 5500);
  11. // logger.logger();
  12. // logger.push("customer", "lisi");
  13. // logger.push("product", "ipod");
  14. // logger.push("price", 3000);
  15. // logger.logger();
  16. // 封装logger
  17. // 注意没有 window.Tracker 的情况
  18. import store from "@/store";
  19. // console.log(store);
  20. // console.log(store.state.user);
  21. // export function createNoAuthLog(logs) {
  22. // for (let [k, v] of Object.entries(logs)) {
  23. // logger.push(k, v);
  24. // }
  25. // logger.logger();
  26. // }
  27. // ip 定位?
  28. // 要在用户登录后调用
  29. export function createUserDetailLog(logs) {
  30. try {
  31. const user = store.state.user;
  32. logger.push("userName", user.displayName);
  33. logger.push("userId", user.id);
  34. logger.push("userIdentityNumber", user.identityNumber);
  35. logger.push("userStudentCodeList", user.studentCodeList.join(","));
  36. logger.push("rootOrgName", user.rootOrgName);
  37. logger.push("rootOrgId", user.rootOrgId);
  38. const uuidForEcs = localStorage.getItem("uuidForEcs");
  39. if (uuidForEcs) {
  40. logger.push("uuidForEcs", uuidForEcs);
  41. }
  42. for (let [k, v] of Object.entries(logs)) {
  43. logger.push(k, v);
  44. }
  45. logger.push("clientDate", moment().format("HH:mm:ss.SSS"));
  46. logger.push("UA", navigator.userAgent);
  47. logger.logger();
  48. } catch (error) {
  49. console.log(error);
  50. window._hmt.push(["_trackEvent", "创建用户日志出错-1"]);
  51. }
  52. }
  53. export function createLog(logs) {
  54. try {
  55. const user = store.state.user;
  56. const uuidForEcs = localStorage.getItem("uuidForEcs");
  57. if (uuidForEcs) {
  58. logger.push("uuidForEcs", uuidForEcs);
  59. }
  60. if (user) {
  61. logger.push("userId", user.id);
  62. }
  63. for (let [k, v] of Object.entries(logs)) {
  64. logger.push(k, v);
  65. }
  66. logger.push("clientDate", moment().format("HH:mm:ss.SSS"));
  67. logger.logger();
  68. } catch (error) {
  69. console.log(error);
  70. window._hmt.push(["_trackEvent", "创建用户日志出错-2"]);
  71. }
  72. }
  73. export function createEncryptLog() {
  74. const isElectron = typeof nodeRequire != "undefined";
  75. // 非 electron 返回
  76. if (!isElectron) return;
  77. try {
  78. const user = store.state.user;
  79. const uuidForEcs = localStorage.getItem("uuidForEcs");
  80. if (uuidForEcs) {
  81. logger.push("uuidForEcs", uuidForEcs);
  82. } else {
  83. // 没有 uuidForEcs 日志没法查询
  84. return;
  85. }
  86. if (user) {
  87. logger.push("userId", user.id);
  88. }
  89. let log = null;
  90. try {
  91. let lastLogIndex = (localStorage.getItem("lastLogIndex") || 0) - 0;
  92. log = window.nodeRequire("electron-log");
  93. // const filePath = log.getFile().path;
  94. const filePath = log.transports.file.findLogPath();
  95. const fs = window.nodeRequire("fs");
  96. const content = fs.readFileSync(filePath, "utf-8");
  97. const ary = content.toString().split("\r\n").join("\n").split("\n");
  98. // 重复上传没有时间的行:跨过非时间戳的行; 错误识别:全部重新执行
  99. // let lastIndex = ary.findIndex(v => v === lastLog);
  100. // console.log({ lastIndex });
  101. if (ary.length < lastLogIndex) {
  102. lastLogIndex = 0;
  103. }
  104. let logLen = 10;
  105. const newAry = ary
  106. .slice(lastLogIndex, lastLogIndex + logLen)
  107. .filter((v) => v);
  108. // 如果没有上传的内容,则不修改lastLog, 也不上传
  109. if (!newAry.length) return;
  110. lastLogIndex = lastLogIndex + newAry.length;
  111. localStorage.setItem("lastLogIndex", lastLogIndex);
  112. logger.push("encryptLog", newAry.join("\n"));
  113. } catch (error) {
  114. console.debug(error);
  115. return;
  116. }
  117. logger.push("clientDate", moment().format("HH:mm:ss.SSS"));
  118. logger.logger();
  119. } catch (error) {
  120. console.log(error);
  121. window._hmt.push(["_trackEvent", "创建用户日志出错-3"]);
  122. }
  123. }
  124. // const postLog = body => {
  125. // const headers = new Headers();
  126. // headers.append("x-log-apiversion", "0.6.0");
  127. // fetch(`https://${project}.${host}/logstores/${logstore}`, {
  128. // method: "post",
  129. // headers,
  130. // body,
  131. // });
  132. // };
  133. // export function createEncryptLog() {
  134. // try {
  135. // const user = store.state.user;
  136. // const uuidForEcs = localStorage.getItem("uuidForEcs");
  137. // const singleLog = {};
  138. // const __logs__ = [singleLog];
  139. // if (uuidForEcs) {
  140. // singleLog.uuidForEcs = uuidForEcs;
  141. // } else {
  142. // // 没有 uuidForEcs 日志没法查询
  143. // return;
  144. // }
  145. // if (user) {
  146. // singleLog.userId = user.id;
  147. // }
  148. // const isElectron = typeof nodeRequire != "undefined";
  149. // let log = null;
  150. // if (isElectron) {
  151. // try {
  152. // log = window.nodeRequire("electron-log");
  153. // // const filePath = log.getFile().path;
  154. // const filePath = log.transports.file.findLogPath();
  155. // const fs = window.nodeRequire("fs");
  156. // const encryptLog = fs.readFileSync(filePath, "utf-8");
  157. // singleLog.encryptLog = encryptLog;
  158. // postLog(JSON.stringify({ __logs__ }));
  159. // } catch (error) {
  160. // console.debug(error);
  161. // return;
  162. // }
  163. // }
  164. // singleLog.clientDate = moment().format("HH:mm:ss.SSS");
  165. // postLog(__logs__);
  166. // } catch (error) {
  167. // console.log(error);
  168. // window._hmt.push(["_trackEvent", "创建用户日志出错-3"]);
  169. // }
  170. // }