Java實(shí)時(shí)監(jiān)控日志文件并輸出的方法詳解
前言
最近有一個(gè)銀行數(shù)據(jù)漂白系統(tǒng),要求操作人員在頁(yè)面調(diào)用遠(yuǎn)端Linux服務(wù)器的shell,并將shell輸出的信息保存到一個(gè)日志文件,前臺(tái)頁(yè)面要實(shí)時(shí)顯示日志文件的內(nèi)容.這個(gè)問題難點(diǎn)在于如何判斷哪些數(shù)據(jù)是新增加的,通過查看JDK 的幫助文檔,
java.io.RandomAccessFile可以解決這個(gè)問題.為了模擬這個(gè)問題,編寫LogSvr和 LogView類,LogSvr不斷向mock.log日志文件寫數(shù)據(jù),而 LogView則實(shí)時(shí)輸出日志變化部分的數(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();
}
//啟動(dòng)一個(gè)線程每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; //上次文件大小
/**
* 實(shí)時(shí)輸出日志信息
* @param logFile 日志文件
* @throws IOException
*/
public void realtimeShowLog(File logFile) throws IOException{
//指定文件可讀可寫
final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");
//啟動(dòng)一個(gè)線程每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類會(huì)啟動(dòng)一個(gè)線程,每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:
代碼修改過, 有朋友下載了我的代碼,說如果是中文會(huì)亂碼,將日志輸出類的第30行的代碼 System.out.println(tmp)改成 System.out.println(new String(tmp.getBytes("ISO8859-1"))) ,就會(huì)正常顯示中文.
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)詳解
在現(xiàn)代的Web應(yīng)用中,緩存是提高系統(tǒng)性能和響應(yīng)速度的重要手段之一,Spring框架提供了強(qiáng)大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實(shí)現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)2025-01-01
如何使用攔截器獲取請(qǐng)求的入?yún)⒉⑵滢D(zhuǎn)化為Java對(duì)象詳解
這篇文章主要介紹了如何使用攔截器獲取請(qǐng)求的入?yún)⒉⑵滢D(zhuǎn)化為Java對(duì)象的相關(guān)資料,文中介紹了兩種實(shí)現(xiàn)的方法,并給出了詳細(xì)的代碼示例,需要的朋友可以參考下2025-02-02
Mybatis統(tǒng)計(jì)sql運(yùn)行時(shí)間的兩種方式
這篇文章主要介紹了Mybatis統(tǒng)計(jì)sql運(yùn)行時(shí)間的方案,Spring?Boot?+?Mybatis?web項(xiàng)目,統(tǒng)計(jì)sql運(yùn)行時(shí)間,用于分析慢sql,優(yōu)化系統(tǒng)速度,方案有兩種:自定義實(shí)現(xiàn)?Interceptor和使用現(xiàn)有依賴庫(kù)(Druid),文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-11-11
SpringBoot中過濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證
本文主要介紹了SpringBoot中過濾器Filter+JWT令牌實(shí)現(xiàn)登錄驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
Java中bcrypt算法實(shí)現(xiàn)密碼加密的方法步驟
我們可以在Spring Boot和SSM中實(shí)現(xiàn)密碼加密,使用bcrypt算法可以保障密碼的安全性,并且減少了手動(dòng)編寫哈希函數(shù)的工作量,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08
SpringBoot3讀取配置文件application.properties屬性值的幾種方式
這篇文章主要介紹了SpringBoot3讀取配置文件application.properties屬性值的幾種方式,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11

