这篇文章主要给大家介绍了关于java实时监控日志文件并输出的方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
前言
最近有一个银行数据漂白系统,要求操作人员在页面调用远端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))) ,就会正常显示中文.
以上就是java中实时监控日志文件并输出的方法实例详解的详细内容。