Commit 0214d765 by sxy

feat: add log

parent 190d04f8
...@@ -4,6 +4,7 @@ const settings = require("../../config/settings"); ...@@ -4,6 +4,7 @@ const settings = require("../../config/settings");
class APIBase { class APIBase {
constructor() { constructor() {
this.cacheManager = system.getObject("db.common.cacheManager"); this.cacheManager = system.getObject("db.common.cacheManager");
this.logClient = system.getObject("util.logClient");
} }
async setContextParams(pobj, qobj, req) { async setContextParams(pobj, qobj, req) {
let custtags = req.headers["x-consumetag"] ? req.headers["x-consumetag"].split("|") : null; let custtags = req.headers["x-consumetag"] ? req.headers["x-consumetag"].split("|") : null;
...@@ -39,10 +40,11 @@ class APIBase { ...@@ -39,10 +40,11 @@ class APIBase {
if (xarg && xarg[0] < 0) { if (xarg && xarg[0] < 0) {
return system.getResultFail(...xarg); return system.getResultFail(...xarg);
} }
var rtn = await this[methodname](pobj, query, req); var rtn = await this[methodname](pobj, query, req);
this.logClient.log(pobj, req, rtn)
return rtn; return rtn;
} catch (e) { } catch (e) {
this.logClient.log(pobj, req, null, e.stack);
console.log(e.stack, "api调用异常--error..................."); console.log(e.stack, "api调用异常--error...................");
var rtnerror = system.getResultFail(-200, "出现异常,请联系管理员"); var rtnerror = system.getResultFail(-200, "出现异常,请联系管理员");
return rtnerror; return rtnerror;
......
...@@ -6,6 +6,7 @@ class CtlBase { ...@@ -6,6 +6,7 @@ class CtlBase {
this.serviceName = sname; this.serviceName = sname;
this.service = system.getObject("service." + gname + "." + sname); this.service = system.getObject("service." + gname + "." + sname);
this.cacheManager = system.getObject("db.common.cacheManager"); this.cacheManager = system.getObject("db.common.cacheManager");
this.logClient = system.getObject("util.logClient");
} }
static getServiceName(ClassObj) { static getServiceName(ClassObj) {
...@@ -53,6 +54,8 @@ class CtlBase { ...@@ -53,6 +54,8 @@ class CtlBase {
bizpath: req.headers["xbizpath"], bizpath: req.headers["xbizpath"],
opath: req.headers['xopath'], opath: req.headers['xopath'],
ptags: req.headers['xptags'], ptags: req.headers['xptags'],
codename: req.headers["xcodename"],
codetitle: req.headers["xcodetitle"] ? decodeURI(req.headers["xcodetitle"]) : '',
} }
if (req.xctx.ptags && req.xctx.ptags != "") { if (req.xctx.ptags && req.xctx.ptags != "") {
pobj.opath = req.xctx.ptags pobj.opath = req.xctx.ptags
...@@ -107,8 +110,10 @@ class CtlBase { ...@@ -107,8 +110,10 @@ class CtlBase {
// } // }
//req.session=redis缓存的上下文对象 //req.session=redis缓存的上下文对象
var rtn = await this[methodname](pobj, query, req); var rtn = await this[methodname](pobj, query, req);
this.logClient.log(pobj, req, rtn)
return rtn; return rtn;
} catch (e) { } catch (e) {
this.logClient.log(pobj, req, null, e.stack);
console.log(e.stack, "出现异常,请联系管理员......."); console.log(e.stack, "出现异常,请联系管理员.......");
return system.getResultFail(-200, "出现异常,请联系管理员"); return system.getResultFail(-200, "出现异常,请联系管理员");
} }
......
...@@ -70,11 +70,11 @@ class System { ...@@ -70,11 +70,11 @@ class System {
method: md, method: md,
json: true, json: true,
headers: { headers: {
"content-type": "application/json", 'Content-type': 'application/json',
// 'Authorization': 'Basic YWRtaW5lczphZG1pbkdTQmVzLg=='
}, },
body: data body: data
}, function (error, response, body) { }, function (error, response, body) {
console.log(error)
rtn.statusCode = response.statusCode rtn.statusCode = response.statusCode
if (!error) { if (!error) {
if (body) { if (body) {
...@@ -258,6 +258,46 @@ class System { ...@@ -258,6 +258,46 @@ class System {
}; };
/**
* 记录日志信息
* @param {*} opTitle 操作的标题
* @param {*} params 参数
* @param {*} identifyCode 业务标识
* @param {*} resultInfo 返回结果
* @param {*} errorInfo 错误信息
*/
static execLogs(opTitle, params, identifyCode, resultInfo, errorInfo) {
var reqUrl = settings.logUrl();
let isLogData = true
if (params.method && (params.method.indexOf("find") >= 0 || params.method.indexOf("get") >= 0)) {
isLogData = false
}
var param = {
actionType: "produceLogsData",// Y 功能名称
actionBody: {
opTitle: opTitle || "",// N 操作的业务标题
identifyCode: identifyCode || "brg-center-manage",// Y 操作的业务标识
indexName: settings.logindex,// Y es索引值,同一个项目用一个值
messageBody: params, //日志的描述信息
resultInfo: isLogData ? resultInfo : { status: resultInfo.status },//返回信息
errorInfo: errorInfo,//错误信息
requestId: resultInfo.requestId || ""
}
};
console.log(JSON.stringify(param))
let P = new Promise((resv, rej) => {
this.postJsonTypeReq(reqUrl, param).then(res => {
if (res.statusCode == 200) {
resv(res.data)
} else {
rej(null)
}
});
})
return P
}
} }
Date.prototype.Format = function (fmt) { //author: meizz Date.prototype.Format = function (fmt) { //author: meizz
var o = { var o = {
......
// var log4js = require('log4js'); // var log4js = require('log4js');
var settings=require("../../config/settings"); var settings = require("../../config/settings");
class LogClient{ const uuidv4 = require('uuid/v4');
constructor(){ const system = require("../system");
// log4js.configure(settings.basepath+"/app/config/log4js.json"); class LogClient {
// this.logerApp=log4js.getLogger("app"); constructor() {
// this.logerHttp=log4js.getLogger("http"); }
getUUID() {
var uuid = uuidv4();
var u = uuid.replace(/\-/g, "");
return u;
}
async log(pobj, req, rtninfo, errinfo) {
rtninfo.requestId = this.getUUID()
req.params.param = pobj
//第三个字段应该存公司id
system.execLogs(settings.appname + "_" + req.xctx.codetitle, req.params, "_" + pobj.company_id + "_", rtninfo, errinfo).then(res => {
if (res && res.status == 1) {
console.log("log.....success")
} else {
console.log("log.....fail")
}
}).catch(e => {
console.log("log.....fail")
})
} }
} }
module.exports=LogClient; module.exports = LogClient;
...@@ -17,9 +17,20 @@ var settings = { ...@@ -17,9 +17,20 @@ var settings = {
defaultpwd: "gsb2020", defaultpwd: "gsb2020",
basepath: path.normalize(path.join(__dirname, '../..')), basepath: path.normalize(path.join(__dirname, '../..')),
port: process.env.NODE_PORT || 8002, port: process.env.NODE_PORT || 8002,
logindex: "center_manage",
appname: "icp_deliver",
kongurl: function () { if (this.env == "dev") { var localsettings = require("./localsettings"); return localsettings.kongurl; } else { return ENVINPUT.KONG_ADMIAN; } }, kongurl: function () { if (this.env == "dev") { var localsettings = require("./localsettings"); return localsettings.kongurl; } else { return ENVINPUT.KONG_ADMIAN; } },
txurl: function () { if (this.env == "dev") { var localsettings = require("./localsettings"); return localsettings.txurl; } txurl: function () {
else { return "http://brg-user-center-service"; } }, if (this.env == "dev") { var localsettings = require("./localsettings"); return localsettings.txurl; }
else { return "http://brg-user-center-service"; }
},
logUrl: function () {
if (this.env == "dev") {
return "http://43.247.184.94:7200/api/queueAction/producer/springBoard";
} else {
return "http://logs-sytxpublic-msgq-service/api/queueAction/producer/springBoard";
}
},
pmappid: 1, pmappid: 1,
pmcompanyid: 1, pmcompanyid: 1,
pmroleid: { "ta": 1, "pr": 2 }, pmroleid: { "ta": 1, "pr": 2 },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment