当前位置: 移动技术网 > IT编程>开发语言>Java > Java实时监控日志文件并输出的方法详解

Java实时监控日志文件并输出的方法详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

工人先进事迹,王梦溪全集,close to you 伴奏

前言

最近有一个银行数据漂白系统,要求操作人员在页面调用远端linux服务器的shell,并将shell输出的信息保存到一个日志文件,前台页面要实时显示日志文件的内容.这个问题难点在于如何判断哪些数据是新增加的,通过查看jdk 的帮助文档,

java.io.randomaccessfile可以解决这个问题.为了模拟这个问题,编写logsvr和 logview类,logsvr不断向mock.log日志文件写数据,而 logview则实时输出日志变化部分的数据.

代码1:日志产生类

package com.bill99.seashell.domain.svr; 
 
import java.io.file; 
import java.io.filewriter; 
import java.io.ioexception; 
import java.io.writer; 
import java.text.simpledateformat; 
import java.util.date; 
import java.util.concurrent.executors; 
import java.util.concurrent.scheduledexecutorservice; 
import java.util.concurrent.timeunit; 
/** 
 *<p>title: 日志服务器</p> 
 *<p>description: 模拟日志服务器</p> 
 *<p>copyright: copyright (c) 2010</p> 
 *<p>company: 99bill.com</p> 
 *<p>create date: 2010-6-18</p> 
 *@author tank zhang<tank.zhang@99bill.com> 
 *@version v0.1 2010-6-18 
 */ 
public class logsvr { 
  
 private simpledateformat dateformat = 
  new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
 
 /** 
  * 将信息记录到日志文件 
  * @param logfile 日志文件 
  * @param mesinfo 信息 
  * @throws ioexception 
  */ 
 public void logmsg(file logfile,string mesinfo) throws ioexception{ 
  if(logfile == null) { 
   throw new illegalstateexception("logfile can not be null!"); 
  } 
  writer txtwriter = new filewriter(logfile,true); 
  txtwriter.write(dateformat.format(new date()) +"\t"+mesinfo+"\n"); 
  txtwriter.flush(); 
 } 
  
 public static void main(string[] args) throws exception{ 
   
  final logsvr logsvr = new logsvr(); 
  final file tmplogfile = new file("mock.log"); 
  if(!tmplogfile.exists()) { 
   tmplogfile.createnewfile(); 
  } 
  //启动一个线程每5秒钟向日志文件写一次数据 
  scheduledexecutorservice exec = 
   executors.newscheduledthreadpool(1); 
  exec.schedulewithfixeddelay(new runnable(){ 
   public void run() { 
    try { 
     logsvr.logmsg(tmplogfile, " 99bill test !"); 
    } catch (ioexception e) { 
     throw new runtimeexception(e); 
    } 
   } 
  }, 0, 5, timeunit.seconds); 
 } 
} 

 代码2:显示日志的类

package com.bill99.seashell.domain.client;  
 
import java.io.file;  
import java.io.ioexception;  
import java.io.randomaccessfile;  
import java.util.concurrent.executors;  
import java.util.concurrent.scheduledexecutorservice;  
import java.util.concurrent.timeunit;  
 
public class logview {  
 private long lasttimefilesize = 0; //上次文件大小  
 /** 
  * 实时输出日志信息 
  * @param logfile 日志文件 
  * @throws ioexception 
  */ 
 public void realtimeshowlog(file logfile) throws ioexception{  
  //指定文件可读可写  
  final randomaccessfile randomfile = new randomaccessfile(logfile,"rw");  
  //启动一个线程每10秒钟读取新增的日志信息  
  scheduledexecutorservice exec =  
   executors.newscheduledthreadpool(1);  
  exec.schedulewithfixeddelay(new runnable(){  
   public void run() {  
    try {  
     //获得变化部分的  
     randomfile.seek(lasttimefilesize);  
     string tmp = "";  
     while( (tmp = randomfile.readline())!= null) {  
      system.out.println(new string(tmp.getbytes("iso8859-1")));  
     }  
     lasttimefilesize = randomfile.length();  
    } catch (ioexception e) {  
     throw new runtimeexception(e);  
    }  
   }  
  }, 0, 1, timeunit.seconds);  
 }  
   
 public static void main(string[] args) throws exception {  
  logview view = new logview();  
  final file tmplogfile = new file("mock.log");  
  view.realtimeshowlog(tmplogfile);  
 }  
 
} 

执行logsvr类,logsvr类会启动一个线程,每5秒钟向mock.log日志文件写一次数据,然后再执行logview类,logview每隔1秒钟读一次,如果数据有变化则输出变化的部分.

结果输出:

2010-06-19 17:25:54 99bill test !
2010-06-19 17:25:59 99bill test !
2010-06-19 17:26:04 99bill test !
2010-06-19 17:26:09 99bill test !
2010-06-19 17:26:14 99bill test !
2010-06-19 17:26:19 99bill test !

ps:

代码修改过, 有朋友下载了我的代码,说如果是中文会乱码,将日志输出类的第30行的代码 system.out.println(tmp)改成 system.out.println(new string(tmp.getbytes("iso8859-1"))) ,就会正常显示中文.

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网