Java实时监控日志文件并输出 转

最近有一个银行数据漂白系统,要求操作人员在页面调用远端Linux服务器的shell,并将shell输出的信息保存到一个日志文件,前台页面要实时显示日志文件的内容.这个问题难点在于如何判断哪些数据是新增加的,通过查看JDK 的帮助文档,java.io.RandomAccessFile
可以解决这个问题.为了模拟这个问题,编写LogSvr和 LogView类,LogSvr不断向mock.log日志文件写数据,而 LogView则实时输出日志变化部分的数据.

代码1:日志产生类

Java代码  收藏代码
  1. package com.bill99.seashell.domain.svr;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.Date;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.ScheduledExecutorService 
  11. import java.util.concurrent.TimeUnit;  
  12.   
  13. public class LogSvr  
  14.       
  15.     private SimpleDateFormat dateFormat   
  16.         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  17.   
  18.       
  19.     public void logMsg(File logFile,String mesInfo) throws IOException{  
  20.         if(logFile == null 
  21.             throw new IllegalStateException("logFile can not be null!");  
  22.          
  23.         Writer txtWriter new FileWriter(logFile,true);  
  24.         txtWriter.write(dateFormat.format(new Date()) +" "+mesInfo+" ");  
  25.         txtWriter.flush();  
  26.      
  27.       
  28.     public static void main(String[] args) throws Exception{  
  29.           
  30.         final LogSvr logSvr new LogSvr();  
  31.         final File tmpLogFile new File("mock.log");  
  32.         if(!tmpLogFile.exists())  
  33.             tmpLogFile.createNewFile();  
  34.          
  35.         //启动一个线程每5秒钟向日志文件写一次数据  
  36.         ScheduledExecutorService exec   
  37.             Executors.newScheduledThreadPool(1);  
  38.         exec.scheduleWithFixedDelay(new Runnable(){  
  39.             public void run()  
  40.                 try  
  41.                     logSvr.logMsg(tmpLogFile, 99bill test !");  
  42.                 catch (IOException e)  
  43.                     throw new RuntimeException(e);  
  44.                  
  45.              
  46.         }, 05TimeUnit.SECONDS);  
  47.      
  48.  

 代码2:显示日志的类

 

 

Java代码  收藏代码
  1. package com.bill99.seashell.domain.client;     
  2.     
  3. import java.io.File;     
  4. import java.io.IOException;     
  5. import java.io.RandomAccessFile;     
  6. import java.util.concurrent.Executors;     
  7. import java.util.concurrent.ScheduledExecutorService    
  8. import java.util.concurrent.TimeUnit;     
  9.     
  10. public class LogView     
  11.     private long lastTimeFileSize 0 //上次文件大小     
  12.         
  13.     public void realtimeShowLog(File logFile) throws IOException{     
  14.         //指定文件可读可写     
  15.         final RandomAccessFile randomFile new RandomAccessFile(logFile,"rw");     
  16.         //启动一个线程每10秒钟读取新增的日志信息     
  17.         ScheduledExecutorService exec      
  18.             Executors.newScheduledThreadPool(1);     
  19.         exec.scheduleWithFixedDelay(new Runnable(){     
  20.             public void run()     
  21.                 try     
  22.                     //获得变化部分的     
  23.                     randomFile.seek(lastTimeFileSize);     
  24.                     String tmp ""    
  25.                     while(tmp randomFile.readLine())!= null    
  26.                         System.out.println(new String(tmp.getBytes("ISO8859-1")));     
  27.                         
  28.                     lastTimeFileSize randomFile.length();     
  29.                 catch (IOException e)     
  30.                     throw new RuntimeException(e);     
  31.                     
  32.                 
  33.         }, 01TimeUnit.SECONDS);     
  34.         
  35.          
  36.     public static void main(String[] args) throws Exception     
  37.         LogView view new LogView();     
  38.         final File tmpLogFile new File("mock.log");     
  39.         view.realtimeShowLog(tmpLogFile);     
  40.         
  41.     
  42.    

 

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

原文地址:https://www.cnblogs.com/luckForever/p/7254243.html