Browse Source

新增日志

zhangjie 4 years ago
parent
commit
08701333cb

+ 1 - 0
.gitignore

@@ -14,6 +14,7 @@ extra/zxing/log*
 npm-debug.log*
 yarn-debug.log*
 yarn-error.log*
+logs*
 
 # Editor directories and files
 .idea

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
     "deepmerge": "^4.2.2",
     "gm": "^1.23.1",
     "imagemagick": "^0.1.3",
+    "log4js": "^6.3.0",
     "sqlite3": "^4.2.0",
     "view-design": "^4.2.0",
     "vue": "^2.6.11",

+ 17 - 25
src/main.js

@@ -16,6 +16,8 @@ import "cropperjs/dist/cropper.min.css";
 import "./assets/styles/index.less";
 
 import { initConfigData } from "@/plugins/env";
+import log4js from "./plugins/logger";
+const logger = log4js.getLogger("request");
 
 Vue.use(ViewUI);
 ViewUI.Message.config({
@@ -30,31 +32,16 @@ Vue.prototype.GLOBAL = GLOBAL;
 
 Vue.config.productionTip = false;
 
-// route interceptor
-// router.beforeEach((to, from, next) => {
-//   const token = Vue.ls.get("token");
-//   if (to.meta.noRequire) {
-//     // 不需要登录的页面
-//     if (token) {
-//       // 当前状态是已登录
-//       ViewUI.Notice.info({ title: "您已经登录,已自动跳转到主页", duration: 2 });
-//       next({ name: "Home" });
-//     } else {
-//       next();
-//     }
-//   } else {
-//     // 需要登录的路由
-//     if (token) {
-//       next();
-//     } else {
-//       // 登录失效的处理
-//       Vue.ls.clear();
-//       // Vue.ls.remove("token");
-//       // Vue.ls.remove("user");
-//       next({ name: "Login" });
-//     }
-//   }
-// });
+// logger
+const addLog = (datas, type = "success") => {
+  if (type === "success") {
+    const msg = `${datas.config.url},请求成功`;
+    logger.info(msg);
+  } else {
+    const msg = `${datas.config.url},请求错误,错误信息:${datas.response.data.status} - ${datas.response.data.message}`;
+    logger.error(msg);
+  }
+};
 
 // axios interceptors
 var load = "";
@@ -87,6 +74,8 @@ axios.interceptors.request.use(
     return config;
   },
   error => {
+    // console.log(error);
+    // logger.error("");
     // 关闭loading提示
     // 串联并发请求,延时处理是为防止多个loading实例闪屏。
     setTimeout(() => {
@@ -98,6 +87,7 @@ axios.interceptors.request.use(
 );
 axios.interceptors.response.use(
   response => {
+    addLog(response);
     // 关闭loading提示
     setTimeout(() => {
       queue.shift();
@@ -106,6 +96,8 @@ axios.interceptors.response.use(
     return response;
   },
   error => {
+    addLog(error, "error");
+
     // 关闭loading提示
     setTimeout(() => {
       queue.shift();

+ 3 - 0
src/modules/client/components/ScanExceptionDialog.vue

@@ -111,6 +111,8 @@
 import Cropper from "cropperjs";
 import { getStudentByExamNumber } from "../api";
 const fs = require("fs");
+import log4js from "@/plugins/logger";
+const logger = log4js.getLogger("scan");
 
 export default {
   name: "scan-exception-dialog",
@@ -216,6 +218,7 @@ export default {
     },
     toRescan() {
       fs.unlinkSync(this.curImage.url);
+      logger.info(`05采集结束:[重新采集] ${this.curImage.name}`);
       this.$emit("on-rescan");
       this.cancel();
     },

+ 16 - 0
src/modules/client/views/GroupScan.vue

@@ -108,6 +108,8 @@ import { deepCopy } from "../../../plugins/utils";
 import ScanAreaDialog from "../components/ScanAreaDialog";
 import ScanExceptionDialog from "../components/ScanExceptionDialog";
 import { mapState, mapActions } from "vuex";
+import log4js from "@/plugins/logger";
+const logger = log4js.getLogger("scan");
 
 const fs = require("fs");
 const { ipcRenderer } = require("electron");
@@ -272,6 +274,7 @@ export default {
       });
     },
     async startDecodeTask(codeArea) {
+      logger.info(`01开始采集:${this.curImage.name}`);
       const examNumber = await decodeImageCode(
         this.curImage.url,
         codeArea
@@ -281,8 +284,10 @@ export default {
       });
 
       if (examNumber) {
+        logger.info(`02解析成功:${examNumber}`);
         this.examNumberValid(examNumber);
       } else {
+        logger.error(`02解析失败:该图片无法识别!`);
         // 未解析到考号异常
         this.curException = {
           showAction: true,
@@ -296,8 +301,10 @@ export default {
       const validRes = await this.checkStudentValid(examNumber);
       if (validRes.valid) {
         // 保存扫描到的试卷
+        logger.info(`03考号校验合法:[${type}] ${examNumber}`);
         this.toSaveStudent(examNumber, type);
       } else {
+        logger.error(`03考号校验不合法:[${type}] ${validRes.message}`);
         // 考号不合法异常
         this.curException = {
           showAction: false,
@@ -362,10 +369,14 @@ export default {
         this.getCurCollectConfig()
       ).catch(error => {
         const content = `${this.curStudent.name}的试卷保存失败,请重新扫描!`;
+        logger.errro(`04保存试卷失败:${this.curStudent.name}`);
         this.$Notice.error({ title: "错误提示", desc: content, duration: 0 });
       });
 
       if (result) {
+        logger.info(
+          `04保存试卷成功:${this.curStudent.examNumber} - ${this.curStudent.name}`
+        );
         this.curStudent = Object.assign(this.curStudent, {
           isCurrent: false,
           isClient: true,
@@ -380,6 +391,7 @@ export default {
         // 更新采集数
         this.updateScanList(this.curStudent.examNumber);
       }
+      logger.info(`05采集结束:${this.curImage.name}`);
       // 删除扫描文件,继续开始下一个任务
       fs.unlinkSync(this.curImage.url);
       this.scrollTaskList();
@@ -396,6 +408,7 @@ export default {
         this.holding = false;
         return;
       }
+      logger.info(`09-01整包数据开始保存`);
       // 添加上传任务
       for (let i = 0, len = this.students.length; i < len; i++) {
         const curStudent = this.students[i];
@@ -421,6 +434,7 @@ export default {
           clientUserLoginTime: this.user.loginTime
         });
       }
+      logger.info(`09-02整包数据保存完毕,扫描结束!`);
 
       this.scanList = [];
       this.students = [];
@@ -428,6 +442,7 @@ export default {
       this.restartInitFile();
     },
     allReScan() {
+      logger.warn(`00整包重扫!`);
       this.$Modal.confirm({
         content: "确定要整包重扫吗?",
         onOk: () => {
@@ -475,6 +490,7 @@ export default {
     },
     // scan-exception
     resetConfig() {
+      logger.info(`重新设置采集区域:${this.curImage.name}`);
       this.curCollectConfig = this.getCurCollectConfig();
       this.$refs.ScanAreaDialog.open();
     },

+ 13 - 0
src/modules/client/views/LineScan.vue

@@ -82,6 +82,8 @@ import { deepCopy } from "../../../plugins/utils";
 import ScanAreaDialog from "../components/ScanAreaDialog";
 import ScanExceptionDialog from "../components/ScanExceptionDialog";
 import { mapState, mapMutations, mapActions } from "vuex";
+import log4js from "@/plugins/logger";
+const logger = log4js.getLogger("scan");
 
 const fs = require("fs");
 const { ipcRenderer } = require("electron");
@@ -227,6 +229,7 @@ export default {
       });
     },
     async startDecodeTask(codeArea) {
+      logger.info(`01开始采集:${this.curImage.name}`);
       const examNumber = await decodeImageCode(
         this.curImage.url,
         codeArea
@@ -236,8 +239,10 @@ export default {
       });
 
       if (examNumber) {
+        logger.info(`02解析成功:${examNumber}`);
         this.examNumberValid(examNumber);
       } else {
+        logger.error(`02解析失败:该图片无法识别!`);
         // 未解析到考号异常
         this.curException = {
           showAction: true,
@@ -251,8 +256,10 @@ export default {
       const validRes = await this.checkStudentValid(examNumber);
       if (validRes.valid) {
         // 保存扫描到的试卷
+        logger.info(`03考号校验合法:[${type}] ${examNumber}`);
         this.toSaveStudent(examNumber, type);
       } else {
+        logger.error(`03考号校验不合法:[${type}] ${validRes.message}`);
         // 考号不合法异常
         this.curException = {
           showAction: false,
@@ -298,10 +305,14 @@ export default {
         this.getCurCollectConfig()
       ).catch(error => {
         const content = `${this.curStudent.name}的试卷保存失败,请重新扫描!`;
+        logger.errro(`04保存试卷失败:${this.curStudent.name}`);
         this.$Notice.error({ title: "错误提示", desc: content, duration: 0 });
       });
 
       if (result) {
+        logger.info(
+          `04保存试卷成功:${this.curStudent.examNumber} - ${this.curStudent.name}`
+        );
         this.curStudent = Object.assign(this.curStudent, {
           isClient: true,
           originImgPath: result.outputOriginPath,
@@ -314,6 +325,7 @@ export default {
         await this.updateScanNo();
         this.initHistory();
       }
+      logger.info(`05采集结束:${this.curImage.name}`);
       // 删除扫描文件,继续开始下一个任务
       fs.unlinkSync(this.curImage.url);
       this.restartInitFile();
@@ -376,6 +388,7 @@ export default {
     },
     // scan-exception
     resetConfig() {
+      logger.info(`重新设置采集区域:${this.curImage.name}`);
       this.curCollectConfig = this.getCurCollectConfig();
       this.$refs.ScanAreaDialog.open();
     },

+ 21 - 0
src/plugins/logger.js

@@ -0,0 +1,21 @@
+import { getHomeDir } from "./env";
+const log4js = require("log4js");
+const path = require("path");
+
+const MAX_SIZE = 10 * 1024 * 1024;
+
+const logDir = getHomeDir("logs");
+const logsFilePath = path.join(logDir, "scan.log");
+
+log4js.configure({
+  appenders: {
+    scan: {
+      type: "file",
+      filename: logsFilePath,
+      maxLogSize: MAX_SIZE
+    }
+  },
+  categories: { default: { appenders: ["scan"], level: "debug" } }
+});
+
+export default log4js;

+ 36 - 1
yarn.lock

@@ -3199,6 +3199,16 @@ date-fns@^1.27.2:
   resolved "https://registry.npm.taobao.org/date-fns/download/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
   integrity sha1-LnG/CxGRU9u0zE6I2epaz7UNwFw=
 
+date-format@^2.1.0:
+  version "2.1.0"
+  resolved "https://registry.npm.taobao.org/date-format/download/date-format-2.1.0.tgz#31d5b5ea211cf5fd764cd38baf9d033df7e125cf"
+  integrity sha1-MdW16iEc9f12TNOLr50DPffhJc8=
+
+date-format@^3.0.0:
+  version "3.0.0"
+  resolved "https://registry.npm.taobao.org/date-format/download/date-format-3.0.0.tgz#eb8780365c7d2b1511078fb491e6479780f3ad95"
+  integrity sha1-64eANlx9KxURB4+0keZHl4DzrZU=
+
 de-indent@^1.0.2:
   version "1.0.2"
   resolved "https://registry.npm.taobao.org/de-indent/download/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"
@@ -4411,7 +4421,7 @@ flat-cache@^2.0.1:
     rimraf "2.6.3"
     write "1.0.3"
 
-flatted@^2.0.0:
+flatted@^2.0.0, flatted@^2.0.1:
   version "2.0.2"
   resolved "https://registry.npm.taobao.org/flatted/download/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138"
   integrity sha1-RXWyHivO50NKqb5mL0t7X5wrUTg=
@@ -6229,6 +6239,17 @@ log-update@^2.3.0:
     cli-cursor "^2.0.0"
     wrap-ansi "^3.0.1"
 
+log4js@^6.3.0:
+  version "6.3.0"
+  resolved "https://registry.npm.taobao.org/log4js/download/log4js-6.3.0.tgz#10dfafbb434351a3e30277a00b9879446f715bcb"
+  integrity sha1-EN+vu0NDUaPjAnegC5h5RG9xW8s=
+  dependencies:
+    date-format "^3.0.0"
+    debug "^4.1.1"
+    flatted "^2.0.1"
+    rfdc "^1.1.4"
+    streamroller "^2.2.4"
+
 loglevel@^1.6.8:
   version "1.6.8"
   resolved "https://registry.npm.taobao.org/loglevel/download/loglevel-1.6.8.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Floglevel%2Fdownload%2Floglevel-1.6.8.tgz#8a25fb75d092230ecd4457270d80b54e28011171"
@@ -8432,6 +8453,11 @@ reusify@^1.0.4:
   resolved "https://registry.npm.taobao.org/reusify/download/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
   integrity sha1-kNo4Kx4SbvwCFG6QhFqI2xKSXXY=
 
+rfdc@^1.1.4:
+  version "1.1.4"
+  resolved "https://registry.npm.taobao.org/rfdc/download/rfdc-1.1.4.tgz#ba72cc1367a0ccd9cf81a870b3b58bd3ad07f8c2"
+  integrity sha1-unLME2egzNnPgahws7WL060H+MI=
+
 rgb-regex@^1.0.1:
   version "1.0.1"
   resolved "https://registry.npm.taobao.org/rgb-regex/download/rgb-regex-1.0.1.tgz#c0e0d6882df0e23be254a475e8edd41915feaeb1"
@@ -9077,6 +9103,15 @@ stream-shift@^1.0.0:
   resolved "https://registry.npm.taobao.org/stream-shift/download/stream-shift-1.0.1.tgz?cache=0&sync_timestamp=1576147145118&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fstream-shift%2Fdownload%2Fstream-shift-1.0.1.tgz#d7088281559ab2778424279b0877da3c392d5a3d"
   integrity sha1-1wiCgVWasneEJCebCHfaPDktWj0=
 
+streamroller@^2.2.4:
+  version "2.2.4"
+  resolved "https://registry.npm.taobao.org/streamroller/download/streamroller-2.2.4.tgz#c198ced42db94086a6193608187ce80a5f2b0e53"
+  integrity sha1-wZjO1C25QIamGTYIGHzoCl8rDlM=
+  dependencies:
+    date-format "^2.1.0"
+    debug "^4.1.1"
+    fs-extra "^8.1.0"
+
 strict-uri-encode@^1.0.0:
   version "1.1.0"
   resolved "https://registry.npm.taobao.org/strict-uri-encode/download/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"