nodejs怎么获取当前请求的日志
1个回答
2017-12-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
function readLog(date,type,operation){//函数接受3个参数,日期、精度(天时分秒)和操作(查找、刷新或者默认) if(date){ //如果有时间参数则对时间参数进行处理(一般日志名称都是时间加类型) var dataStr = arguments[0]; var date = dataStr.slice(0,10); var hour = dataStr.slice(11,13); var minute = dataStr.slice(11,16); var second =dataStr.slice(11,19); var type = arguments[1]; //获取精度
}else{ //若无时间参数则获取当前时间,然后精度默认 var date = new Date(+new Date()+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'').slice(0,10); var type=null;
} var op = arguments[2]; var countResult = 0; //对日志结果进行统计 var logPath = appDirName.slice(0,-3)+'log\\'+'ivsvr_'+date+'.log'; //拼接日志文件名称 var logStr = '';
fs.stat(logPath, function (err, stats) { //获取对应的日志文件状态 if(err){ //日志不存在或者异常处理信息
$('#logTipInfo tr td').html('无日志信息可供查看'); console.log(err);
}else{ var lineCount = 0; var readline = require('readline'); var fReadName = logPath; var fRead = fs.createReadStream(fReadName); var objReadline = readline.createInterface({ input: fRead,
}); //按行读取日志
objReadline.on('line', (line)=>{ var itemStr = $.trim(line); var time = itemStr.slice(0,19); var content = itemStr.substr(20); if(!time || !content){return;}
++ lineCount; //计数行数 var matched = true; var itemStr = $.trim(line); var itemArr = itemStr.split(' '); var logDate = itemArr[0]; var logTime = itemArr[1]; var time = logDate+' '+logTime; switch(type){ //进度查找过滤 case '0': case null:
matched = true; break; case '1': var timeSplit = logTime.slice(0,2); if(timeSplit == hour){
matched = true; break;
}
matched = false; break; case '2': var timeSplit = logTime.slice(0,5); if(timeSplit == minute){
matched = true; break;
}
matched = false; break; case '3': var timeSplit = logTime.slice(0,8); if(timeSplit == second){
matched = true; break;
}
matched = false; break; default: console.log('unkonw type!'); break;
} if(matched === true &&(lineCount>logReadFlag)){ //日志结果格式化输出呈现
countResult ++; var result = itemArr[2]; if(result == 'err'){ var operation = itemArr[4];
result = '失败:'+itemArr[5]+' '+(itemArr[6]?itemArr[6]:'');
}else{ var operation = itemArr[4]+' '+itemArr[5]+(itemArr[6]?itemArr[6]:'');
result = '成功';
}
logStr += '<tr><td>'+time+'</td><td>'+operation+'</td><td>'+result+'</td></tr>'
}
logStr += '<tr><td>'+time+'</td><td>'+content+'</td></tr>'
}); objReadline.on('close', ()=>{ if(logReadFlag == 0 && (logStr == '')){ var info = '无日志信息可供查看';
} if(logReadFlag >0 && (logStr != '')){ var newLogNum = lineCount - logReadFlag; var info = '刷新成功,更新'+newLogNum+'条日志!';
$('#logInfoTable').prepend(logStr);
}else if(logReadFlag >0 && (logStr == '')){ var info = '刷新成功,无日志更新!';
}else{
$('#logInfoTable').html(logStr);
} if(op == 'search'){ var info = '查找到'+countResult+'条日志';
}
$('#logTipInfo tr td').html(info); if((op == 'search' || op == 'refresh') &&(logReadFlag != 0 || newLogNum >0 || countResult >0)){
setTimeout(function() {$('#logTipInfo').fadeOut(567);}, 2000); //操作结束后给出信息反馈,反馈自动淡出消失
} else{
$('#logTipInfo').hide();
}
logReadFlag = lineCount; console.log('closed');
});
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询