LogManager.js
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
*
* LOG 信息管理(上传和输出)
* */
class LogManager {
constructor() {
}
/*
* 添加需要上报的日志信息到队列中
* _type
* _msg
* */
static addLog(_type, _msg) {
//无效的数据不上报
if (!_msg) {
return;
}
if (this.IS_OPEN_SEND_LOG == true || this.IS_OPEN_SEND_LOG == "true") {
//显示格式20170729 16:42:00 INFO _msg
let sendMsgStr = "";
switch (_type) {
case this.ERROR:
sendMsgStr = this.getCurrentDateTime() + " ERROR " + _msg;
//this.errorList.push(sendMsgStr);
break;
case this.WARN:
sendMsgStr = this.getCurrentDateTime() + " WARN " + _msg;
//this.warnList.push(sendMsgStr);
break;
case this.LOG:
sendMsgStr = this.getCurrentDateTime() + " INFO " + _msg;
// this.logList.push(sendMsgStr);
break;
default:
sendMsgStr = this.getCurrentDateTime() + " INFO " + _msg;
// this.logList.push(sendMsgStr);
break;
}
this.allLogList.push(sendMsgStr);
let _this = this;
clearTimeout(this.logDelayTimer);
this.logDelayTimer = setTimeout(function () {
_this.checkAndSendLog();
}, 2000);
}
}
static checkAndSendLog() {
//发送日志
/*this.sendLogToServer(LogManager.ERROR);
this.sendLogToServer(LogManager.WARN);
this.sendLogToServer(LogManager.LOG);*/
//不区分日志类型,按时间先后顺序发送
this.sendLogToServer(-1);
}
//发送log到服务器
static sendLogToServer(_msgType) {
if (!this.logUrl) {
console.warn("日志服务器地址->"+this.logUrl);
return;
}
const msgType = _msgType;
let msgData = "";
let tempArr = [];
let msgLen = 0;
msgLen = this.allLogList.length;
for (let i = 0; i < msgLen; i++) {
let item = this.allLogList.shift();
if (item) {
tempArr.push(item);
msgData += item + "\n ";
}
}
if (tempArr.length < 1 || !msgData) {
//console.log("没有数据->不需要上报");
return;
}
//console.log("上报的日志->", tempArr, msgData);
//需要过滤掉&字符,否则Sass无法取数据
msgData=msgData.replace(/&/g,"#");
const userRoleAndUserId="["+this.userRole+"]_"+this.platform+"__"+this.userId;//身份+userId
fetch(encodeURI(this.logUrl), {
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
},
body: `classId=${this.classId}&userId=${userRoleAndUserId}&nodeId=${this.nodeId}&type=${msgType}&data=${msgData}`,
timeout: 3000
})
.then(ret => {
if (ret.ok) {
return ret.json();
} else {
console.error(`保存日志信息-网络异常.状态码:${ret.status}`);
throw '';
}
})
.then(ret => {
if (ret == 0) {
//console.log('保存日志信息 完成');
tempArr=[];
} else {
console.warn('保存日志信息 失败.', ret);
this.allLogList=tempArr.concat(this.allLogList);
tempArr=[];
}
})
.catch(err => {
console.error(`保存日志信息.状态码:${err}`);
this.allLogList=tempArr.concat(this.allLogList);
tempArr=[]
});
}
//计算当前服务器时间
static getCurrentDateTime() {
let currentServerTime = new Date().getTime() - this.serverAndLoacTimeDistanc * 1000;//计算当前服务器时间
let time=new Date(currentServerTime);
//显示格式20170729 16:42:00 INFO _msg
let timeStr = time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
return timeStr;
}
}
LogManager.allLogList = [];//所有需要上报的日志列表
LogManager.logList = [];//日志上报列表
LogManager.warnList = [];//警告日志上报列表
LogManager.errorList = [];//错误日志上报列表
LogManager.serverAndLoacTimeDistanc = 0;//本地时间和服务器时间的差值(秒)
LogManager.classId = 0;//课堂号
LogManager.userId = "";//userId
LogManager.nodeId = 0;//nodeId
LogManager.userName = "";//用户名称
LogManager.userRole = "normal";//用户名称
LogManager.logUrl = "";//日志服务器地址 //http://log.3mang.com:8888
LogManager.platform="unknow";
LogManager.ERROR = 1;
LogManager.WARN = 2;
LogManager.LOG = 3;
LogManager.DATA = 5;
LogManager.logDelayTimer = 0;
LogManager.IS_OPEN_SEND_LOG = true;//是否上报日志
export default LogManager;