java实时输出日志文件_Java实时监控日志文件并输出(已进行修改,运行没问题)...

这是一个关于Java如何实现实时监控并输出日志文件的示例。`LogSvr` 类用于生成日志,每5秒写入一次数据到`mock.log`文件。而`LogView` 类则负责每1秒检查并打印出文件中新增的日志信息,确保中文字符正确显示。
摘要由CSDN通过智能技术生成

代码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;

/**

*

title: 日志服务器

*

Description: 模拟日志服务器

*

CopyRight: CopyRight (c) 2010

*

Company: 99bill.com

*

Create date: 2010-6-18

*@author Tank Zhang

*@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, " hello, 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");

//启动一个线程每1秒钟读取新增的日志信息

ScheduledExecutorService exec =

Executors.newScheduledThreadPool(1);

exec.scheduleWithFixedDelay(new Runnable(){

public void run() {

try {

//获得变化部分的

randomFile.seek(lastTimeFileSize);

String tmp ="";

tmp = randomAccessFile.readLine();

while(tmp !=null){

System.out.println(new String(tmp.getBytes("iso-8859-1")));

tmp = randomAccessFile.readLine();

}

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  hello test !

2010-06-19 17:25:59  hello test !

2010-06-19 17:26:04  hello test !

2010-06-19 17:26:09  hello test !

2010-06-19 17:26:14  hello test !

2010-06-19 17:26:19  hello test !

PS:

代码修改过, 有朋友下载了我的代码,说如果是中文会乱码,将日志输出类的第30行的代码

System.out.println(tmp)改成 System.out.println(new String(tmp.getBytes("ISO8859-1"))),就会正常显示中文.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值