/** * * LOG 信息管理(上传和输出) * */ const WEBRTC_LOG="WebRtcLog-" class LogManager { constructor() { } //捕获日志输出 static catchConsole(){ console.oldLog = console.log; console.log("捕获日志输出"); console.log = (...args)=> { if(args[0]=="INFO:"||args[0]=="DEBUG:"||args[0]=="WARNING:"||args[0]=="ERROR:") { LogManager.addLog(LogManager.LOG,WEBRTC_LOG+JSON.stringify(args)); }else { if(!args){ return; } if(args.length==1){ console.oldLog(args[0]); }else{ console.oldLog(args); } } } console.warn = (...args)=> { if(args[0]=="INFO:"||args[0]=="DEBUG:"||args[0]=="WARNING:"||args[0]=="ERROR:") { LogManager.addLog(LogManager.LOG,WEBRTC_LOG+JSON.stringify(args)); }else { if(!args){ return; } if(args.length==1){ console.oldLog(args[0]); }else{ console.oldLog(args); } } } console.error = (...args)=> { if(args[0]=="INFO:"||args[0]=="DEBUG:"||args[0]=="WARNING:"||args[0]=="ERROR:") { LogManager.addLog(LogManager.LOG,WEBRTC_LOG+JSON.stringify(args)); }else { if(!args){ return; } if(args.length==1){ console.oldLog(args[0]); }else{ console.oldLog(args); } } } } /* * 添加需要上报的日志信息到队列中 * _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;