In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Java to read log files in real time". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Coding implementation
Write log files, write 200 records per second, and record the time of writing
Import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.text.SimpleDateFormat;import java.util.Date;public class LogReader implements Runnable {private File logFile = null; private long lastTimeFileSize = 0; / Last file size private static SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); public LogReader (File logFile) {this.logFile = logFile; lastTimeFileSize = logFile.length () } / * Real-time output log information * / public void run () {while (true) {try {long len = logFile.length (); if (len
< lastTimeFileSize) { System.out.println("Log file was reset. Restarting logging from start of file."); lastTimeFileSize = len; } else if(len >LastTimeFileSize) {RandomAccessFile randomFile = new RandomAccessFile (logFile, "r"); randomFile.seek (lastTimeFileSize); String tmp = null While ((tmp = randomFile.readLine ())! = null) {System.out.println (dateFormat.format (new Date ()) + "\ t" + tmp);} lastTimeFileSize = randomFile.length (); randomFile.close () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {Thread.sleep (50);} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace () }
Read log files in real time, every 1 second
Import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.text.SimpleDateFormat;import java.util.Date;public class LogReader implements Runnable {private File logFile = null; private long lastTimeFileSize = 0; / Last file size private static SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); public LogReader (File logFile) {this.logFile = logFile; lastTimeFileSize = logFile.length () } / * Real-time output log information * / public void run () {while (true) {try {RandomAccessFile randomFile = new RandomAccessFile (logFile, "r"); randomFile.seek (lastTimeFileSize); String tmp = null While ((tmp = randomFile.readLine ())! = null) {System.out.println (dateFormat.format (new Date ()) + "\ t" + tmp);} lastTimeFileSize = randomFile.length () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {Thread.sleep;} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}
Start the write thread and the reader thread, and print the real-time information on the console.
Import java.io.File;public class RunRun {public static void main (String [] args) {File logFile = new File ("mock.log"); Thread wthread = new Thread (new LogWrite (logFile)); wthread.start (); Thread rthread = new Thread (new LogReader (logFile)); rthread.start ();}}
In the process of reading and writing, we can manually rename the mock.log file and find that it can still be read in real time.
This is the end of "how to use Java to read log files in real time". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.