日志文件操作
要求:页面能实时显示日志文件中的内容(自动更新)
日志文件读取
读取一次后记下当前读取的位置,作为下一次读取的起始点,使用RandomAccessFile来实现断点读取
1 public ResultObject runLog(String id, String pos) { 2 ResultObject rs = new ResultObject(); 3 //日志文件保存路径 4 String path = (String) HspConfiguration.readConfig("logconf").getValue(); 5 File file = new File(path + id); 6 //随机文件读取流 7 RandomAccessFile randomFile = null; 8 Long pageLong = 4096L; 9 //读取一行的缓存 10 String line = ""; 11 //读取的日志文件内容 12 String log = ""; 13 //读取指针 14 String pointer = ""; 15 //日志文件bean 16 LogBean logBean = new LogBean(); 17 try{ 18 // 按只读方式打开一个随机访问文件流 19 randomFile = new RandomAccessFile(file, "r"); 20 //从记录的位置开始读取 21 pointer = String.valueOf(randomFile.length()); 22 pos = pos == null || "".equals(pos) ? "0" : pos; 23 if ("0".equalsIgnoreCase(pos)) { 24 pageLong = randomFile.length(); 25 } 26 randomFile.seek(Long.parseLong(pos)); 27 //开始读取内容 28 while ((line = randomFile.readLine()) != null) { 29 line = new String(line.getBytes("ISO8859-1"), System.getProperty("file.encoding")); 30 if("".equals(log)) { 31 log = setLogColor(line); 32 } else { 33 log += "<br/>" + setLogColor(line); 34 } 35 if((randomFile.getFilePointer() - Long.parseLong(pos)) > pageLong) { 36 pointer = String.valueOf(randomFile.getFilePointer());break; 37 } 38 } 39 } catch (FileNotFoundException e) { 40 rs.setResult(EnumActionRS.ERROR.getRS()); 41 rs.setMessage("未找到日志文件"); 42 return rs; 43 } catch (IOException e) { 44 rs.setResult(EnumActionRS.ERROR.getRS()); 45 rs.setMessage("读取日志文件错误"); 46 return rs; 47 } finally { 48 if (randomFile != null) { 49 try { 50 randomFile.close(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 } 56 logBean.setId(id); 57 logBean.setPos(pointer); 58 logBean.setLog(log); 59 rs.setRcdata(logBean); 60 rs.setResult(EnumActionRS.SUCCESS.getRS()); 61 return rs; 62 }
格式化(标注行的颜色)
public String setLogColor(String line) { String lineWithColor = line; if(line.contains("-- ERROR")) { lineWithColor = "<span style='color:red;'>" + line + "</span>"; } return lineWithColor; }
页面用于显示日志的DIV
<div id="log" style="overflow:auto;font-size:12px;font-family: Courier New;width: 98%;height:700px;margin:0 auto;border:1px solid grey;"> </div>
自动更新和处理错误的JS
$(document).ready(function(){ read = window.setInterval(readLog,1000); readLog(); }); //用于首次读取日志时设置滚动条的位置 var first = true; /* * 读取日志信息 */ function readLog(){ var param = {}; param["logBean.id"] = $("#deviceId").html(); param["logBean.pos"] = $("#pos").val(); ajaxGet(param, "/deviceAjax/runLog.do", readLogR); } function readLogR(data){ if(data.result == 'error'){ $("#log").append('<span style="color:red;">' + data.message + '</span>'); window.clearInterval(read); return; } if(data.returnData == null || data.returnData == undefined){ $("#log").append('<span style="color:red;">请求数据失败...</span><br />'); window.clearInterval(read); return; } var log = data.returnData.log.replace(/ /g, " "); if(log != ''){ $("#log").append(log + '<br />'); } //设置滚动条到底部 if(first){ $("#log").animate({scrollTop:10000},4000); first = false; } //存储当前读取的位置 $("#pos").val(data.returnData.pos); }