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

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

想在前台显示数据同步过程当中产生的日志文件,在网上找到解决方案,作了代码测试好用。这里作个记录html

java.io.RandomAccessFile能够解决同时向文件读和写.为了模拟这个问题,编写LogSvr和 LogView类,LogSvr不断向mock.log日志文件写数据,而 LogView则实时输出日志变化部分的数据.java

代码1:日志产生类

package com.fbkj.genelog;

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;
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();
        String userdir = System.getProperty("user.dir");
        final File tmpLogFile = new File(userdir+"/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.fbkj.genelog;
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 {
        String userdir = System.getProperty("user.dir");
        LogView view = new LogView();
        final File tmpLogFile = new File(userdir+"/mock.log");
        view.realtimeShowLog(tmpLogFile);
    }
}

先执行LogSvr类,LogSvr类会启动一个线程,每5秒钟向mock.log日志文件写一次数据,dom

而后再执行LogView类,LogView每隔1秒钟读一次,若是数据有变化则输出变化的部分.测试

参考 https://www.qedev.com/dev/245197.html spa

相关文章
相关标签/搜索