Prechádzať zdrojové kódy

新增全局日志,便于排查问题

MaiXinRong 2 rokov pred
rodič
commit
af6f0147a3

+ 1 - 0
app/middleware/auto_finish_logger.js

@@ -38,5 +38,6 @@ module.exports = options => {
         }
         const bLogger = runTime > 500 ? ctx.getLogger('warning') : ctx.getLogger('finish');
         bLogger.info(JSON.stringify(logData));
+        await ctx.service.globalLog.responseLog(runTime);
     };
 };

+ 1 - 0
app/middleware/auto_logger.js

@@ -44,6 +44,7 @@ module.exports = options => {
             this.logInfo.cpus = cpus.map(x => {
                 return `${((1-x.times.idle/(x.times.idle+x.times.user+x.times.nice+x.times.sys+x.times.irq))*100).toFixed(2)}%`
             });
+            yield this.service.globalLog.requestLog();
         }
         yield next;
     };

+ 46 - 0
app/service/global_log.js

@@ -0,0 +1,46 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+module.exports = app => {
+    class GlobalLog extends app.BaseService {
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'global_log';
+        }
+
+        async requestLog() {
+            if (!this.ctx.session) return;
+
+            await this.db.insert(this.tableName, {
+                id: this.ctx.logId,
+                project_id: this.ctx.session.sessionProject.id,
+                project_code: this.ctx.session.sessionProject.code,
+                user_id: this.ctx.session.sessionUser.accountId,
+                user_name: this.ctx.session.sessionUser.name,
+                url: this.ctx.url,
+                method: this.ctx.method,
+            });
+        }
+
+        async responseLog(runTime){
+            if (!this.ctx.session) return;
+
+            await this.defaultUpdate({ id: this.ctx.logId, is_response: 1, run_time: runTime });
+        }
+    }
+
+    return GlobalLog;
+};