Commit afc0fb54 by v_vjyjiang

log

parent c36dc90b
var fs = require("fs");
var objsettings = require("../config/objsettings");
var settings = require("../config/settings");
const request = require('request');
class System {
static declare(ns) {
static declare (ns) {
var ar = ns.split('.');
var root = System;
for (var i = 0, len = ar.length; i < len; ++i) {
......@@ -15,7 +16,7 @@ class System {
}
}
}
static register(key, ClassObj) {
static register (key, ClassObj) {
if (System.objTable[key] != null) {
throw new Error("相同key的对象已经存在");
} else {
......@@ -31,7 +32,7 @@ class System {
* @param {*} okmsg 操作成功的描述
* @param {*} req 请求头信息
*/
static getResult(data, opmsg = "操作成功", req) {
static getResult (data, opmsg = "操作成功", req) {
return {
status: !data ? -1 : 0,
msg: opmsg,
......@@ -44,7 +45,7 @@ class System {
* @param {*} data 操作成功返回的数据
* @param {*} okmsg 操作成功的描述
*/
static getResultSuccess(data, okmsg = "success") {
static getResultSuccess (data, okmsg = "success") {
return {
status: 0,
msg: okmsg,
......@@ -57,7 +58,7 @@ class System {
* @param {*} errmsg 操作失败的描述,默认为fail
* @param {*} data 操作失败返回的数据
*/
static getResultFail(status = -1, errmsg = "fail", data = null) {
static getResultFail (status = -1, errmsg = "fail", data = null) {
return {
status: status,
msg: errmsg,
......@@ -69,14 +70,14 @@ class System {
* @param {*} errmsg 操作失败的描述,默认为fail
* @param {*} data 操作失败返回的数据
*/
static getResultError(errmsg = "fail", data = null) {
static getResultError (errmsg = "fail", data = null) {
return {
status: -200,
msg: errmsg,
data: data,
};
}
static getObject(objpath) {
static getObject (objpath) {
var pathArray = objpath.split(".");
var packageName = pathArray[0];
var groupName = pathArray[1];
......@@ -97,7 +98,7 @@ class System {
return System.register(objabspath, ClassObj);
}
}
static getUiConfig(appid) {
static getUiConfig (appid) {
var configPath = settings.basepath + "/app/base/db/metadata/" + appid + "/index.js";
if (settings.env == "dev") {
delete require.cache[configPath];
......@@ -105,7 +106,7 @@ class System {
var configValue = require(configPath);
return configValue;
}
static getUiConfig2(appid) {
static getUiConfig2 (appid) {
var configPath = settings.basepath + "/app/base/db/metadata/index.js";
// if(settings.env=="dev"){
// console.log("delete "+configPath+"cache config");
......@@ -115,7 +116,7 @@ class System {
var configValue = require(configPath);
return configValue[appid];
}
static get_client_ip(req) {
static get_client_ip (req) {
var ip = req.headers['x-forwarded-for'] ||
req.ip ||
req.connection.remoteAddress ||
......@@ -129,6 +130,75 @@ class System {
}
};
static async postJsonTypeReq (url, data, md = "POST") {
let rtn = {}
let promise = new Promise(function (resv, rej) {
request({
url: url,
method: md,
json: true,
headers: {
"content-type": "application/json",
},
body: data
}, function (error, response, body) {
console.log(error)
if (!error) {
rtn.statusCode = response.statusCode
if (body) {
rtn.data = body
} else {
rtn.data = null
}
resv(rtn);
} else {
rej(error)
}
});
})
return promise;
}
/**
* 记录日志信息
* @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
var o = {
......
// var log4js = require('log4js');
var settings = require("../../config/settings");
const uuidv4 = require('uuid/v4');
const system = require("../system");
class LogClient {
constructor() {
}
getUUID () {
var uuid = uuidv4();
var u = uuid.replace(/\-/g, "");
return u;
}
async log (pobj, rtninfo, errinfo) {
rtninfo.requestId = this.getUUID()
//第三个字段应该存公司id{status:0}
system.execLogs(settings.appname, pobj, "_" + 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;
......@@ -14,6 +14,15 @@ var ENVINPUT = {
var settings = {
env: ENVINPUT.APP_ENV,
basepath: path.normalize(path.join(__dirname, '../..')),
logindex: "center_manage",
appname: "center-taskexecutor",
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";
}
},
fkweb: function () {
if (this.env == "dev") {
return "http://localhost:3003/api/rpt/rptApi/saveRptHistory";
......
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