博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实时监控日志文件并输出
阅读量:4155 次
发布时间:2019-05-26

本文共 3846 字,大约阅读时间需要 12 分钟。

    最近有一个银行数据漂白系统,要求操作人员在页面调用远端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;/** *

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, " 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"))),就会正常显示中文.

 

 

转载地址:http://zjkxi.baihongyu.com/

你可能感兴趣的文章
进程创建时信号处理函数处理
查看>>
进程创建时信号处理
查看>>
进程创建时内存描述符处理
查看>>
进程创建时命名空间处理
查看>>
进程创建时IO处理
查看>>
进程创建时线程栈处理
查看>>
进程创建时pid分配
查看>>
进程创建时安全计算处理
查看>>
进程创建时cgroup处理
查看>>
进程创建时共享内存处理
查看>>
idle进程创建
查看>>
内核线程创建
查看>>
linux elf tool readelf
查看>>
linux tool objdump
查看>>
linux tool nm
查看>>
字节对齐
查看>>
把类成员函数封装成线程API所需要的函数
查看>>
HTTP Live Streaming直播(iOS直播)技术分析与实现
查看>>
Ribbon界面图标可以直接用PNG做透明图标
查看>>
向其他软件窗口、控件发送消息的方法
查看>>