Java實時監(jiān)控日志文件并輸出的方法詳解
前言
最近有一個銀行數(shù)據(jù)漂白系統(tǒng),要求操作人員在頁面調(diào)用遠(yuǎn)端Linux服務(wù)器的shell,并將shell輸出的信息保存到一個日志文件,前臺頁面要實時顯示日志文件的內(nèi)容.這個問題難點(diǎn)在于如何判斷哪些數(shù)據(jù)是新增加的,通過查看JDK 的幫助文檔,
java.io.RandomAccessFile
可以解決這個問題.為了模擬這個問題,編寫LogSvr和 LogView類,LogSvr不斷向mock.log日志文件寫數(shù)據(jù),而 LogView則實時輸出日志變化部分的數(shù)據(jù).
代碼1:日志產(chǎn)生類
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: 日志服務(wù)器</p> *<p>Description: 模擬日志服務(wù)器</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秒鐘向日志文件寫一次數(shù)據(jù) 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); } }
執(zhí)行LogSvr類,LogSvr類會啟動一個線程,每5秒鐘向mock.log日志文件寫一次數(shù)據(jù),然后再執(zhí)行LogView類,LogView每隔1秒鐘讀一次,如果數(shù)據(jù)有變化則輸出變化的部分.
結(jié)果輸出:
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")))
,就會正常顯示中文.
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java ArrayList集合詳解(Java動態(tài)數(shù)組)
這篇文章主要介紹了Java ArrayList集合詳解(Java動態(tài)數(shù)組),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10基于javamelody監(jiān)控springboot項目過程詳解
這篇文章主要介紹了基于javamelody監(jiān)控springboot項目過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11