Commit 255ab956 by sxy

feat: add log

parent bd16f91e
...@@ -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;
...@@ -27,7 +28,7 @@ class APIBase { ...@@ -27,7 +28,7 @@ class APIBase {
} }
if (req.xctx.userid) {//在请求传递数据对象注入公司id if (req.xctx.userid) {//在请求传递数据对象注入公司id
pobj.userid = req.xctx.userid; pobj.userid = req.xctx.userid;
pobj.username= req.xctx.username pobj.username = req.xctx.username
} }
if (!req.xctx.appkey) { if (!req.xctx.appkey) {
return [-200, "请求头缺少应用x-app-key"] return [-200, "请求头缺少应用x-app-key"]
...@@ -41,7 +42,7 @@ class APIBase { ...@@ -41,7 +42,7 @@ class APIBase {
// if(!req.xctx.companyid && !req.xctx.companykey){ // if(!req.xctx.companyid && !req.xctx.companykey){
// return [-200,"请求头缺少应用x-app-key"] // return [-200,"请求头缺少应用x-app-key"]
// } // }
if (!req.xctx.companyid && req.xctx.companykey && req.xctx.companykey != 'null' && req.xctx.companykey != 'undefined') { if (!req.xctx.companyid && req.xctx.companykey && req.xctx.companykey != 'null' && req.xctx.companykey != 'undefined') {
let comptmp = await this.cacheManager["CompanyCache"].cache(req.xctx.companykey); let comptmp = await this.cacheManager["CompanyCache"].cache(req.xctx.companykey);
req.xctx.companyid = comptmp.id; req.xctx.companyid = comptmp.id;
} }
...@@ -57,8 +58,10 @@ class APIBase { ...@@ -57,8 +58,10 @@ class APIBase {
} }
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) {
...@@ -52,12 +53,14 @@ class CtlBase { ...@@ -52,12 +53,14 @@ 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
}else{ } else {
pobj.opath=req.xctx.opath pobj.opath = req.xctx.opath
} }
if (!req.xctx.appkey) { if (!req.xctx.appkey) {
return [-200, "请求头缺少应用x-app-key"] return [-200, "请求头缺少应用x-app-key"]
...@@ -82,7 +85,7 @@ class CtlBase { ...@@ -82,7 +85,7 @@ class CtlBase {
} }
if (req.xctx.userid) {//在请求传递数据对象注入公司id if (req.xctx.userid) {//在请求传递数据对象注入公司id
pobj.userid = req.xctx.userid; pobj.userid = req.xctx.userid;
pobj.username= req.xctx.username pobj.username = req.xctx.username
} }
pobj.bizpath = req.xctx.bizpath; pobj.bizpath = req.xctx.bizpath;
} }
...@@ -104,8 +107,10 @@ class CtlBase { ...@@ -104,8 +107,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, "出现异常,请联系管理员");
} }
......
...@@ -257,6 +257,47 @@ class System { ...@@ -257,6 +257,47 @@ 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("写入日志内容:")
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,6 +17,8 @@ var settings = { ...@@ -17,6 +17,8 @@ var settings = {
defaultpwd: "gsb2020", defaultpwd: "gsb2020",
basepath: path.normalize(path.join(__dirname, '../..')), basepath: path.normalize(path.join(__dirname, '../..')),
port: process.env.NODE_PORT || 8888, port: process.env.NODE_PORT || 8888,
logindex: "center_manage",
appname: "ic_deliver",
kongurl: function () { kongurl: function () {
if (this.env == "dev") { if (this.env == "dev") {
var localsettings = require("./localsettings"); var localsettings = require("./localsettings");
...@@ -46,6 +48,13 @@ var settings = { ...@@ -46,6 +48,13 @@ var settings = {
return "http://brg-user-center-service/api/action/order/springBoard"; return "http://brg-user-center-service/api/action/order/springBoard";
} }
}, },
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