Home > Java > Java Tutorial > body text

Detailed explanation of method examples for real-time monitoring and outputting log files in Java

怪我咯
Release: 2017-06-30 10:37:23
Original
2081 people have browsed it

This article mainly introduces you to the method of real-time monitoring and outputting log files in Java. The article introduces it in great detail through sample code, which has certain reference and learning value for everyone. Friends who need it will follow the editor. Study and study.

Preface

Recently there is a bank data bleaching system that requires the operator to call the shell of the remote Linux server on the page and output the shell The information is saved to a log file, and the front page should display the contents of the log file in real time. The difficulty of this problem is how to determine which data is newly added. By viewing the JDK help document,

java.io.RandomAccessFile can solve this problem. In order to simulate this problem, write the LogSvr and LogView classes. LogSvr continuously writes data to the mock.log log file, while LogView outputs the data of the changed part of the log in real time.

Code 1: Log generation class

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); } }
Copy after login

Code 2: Log display class

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);  
 }  
 
}
Copy after login

Execute the LogSvr class. The LogSvr class will start a thread and write data to the mock.log log file every 5 seconds, and then execute the LogView class. LogView will read it every 1 second. If the data changes, output the changed part.

Result output:

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 !
Copy after login

PS:

The code has been modified. A friend downloaded my code and said that if it is in Chinese, it will be garbled. Change the code on line 30 of the log output class System.out.println(tmp) to System.out.println(new String(tmp.getBytes("ISO8859-1"))), Chinese will be displayed normally.


The above is the detailed content of Detailed explanation of method examples for real-time monitoring and outputting log files in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!