JAVA 实现tail -f 日志文件监控功能
工具:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
定义接口
package com.snow.tailer; public interface TailerListener {
/**
* The tailer will call this method during construction,
* giving the listener a method of stopping the tailer.
* @param tailer the tailer.
*/
void init(Tailer tailer); /**
* This method is called if the tailed file is not found.
* <p>
* <b>Note:</b> this is called from the tailer thread.
*/
void fileNotFound(); /**
* Called if a file rotation is detected.
*
* This method is called before the file is reopened, and fileNotFound may
* be called if the new file has not yet been created.
* <p>
* <b>Note:</b> this is called from the tailer thread.
*/
void fileRotated(); /**
* Handles a line from a Tailer.
* <p>
* <b>Note:</b> this is called from the tailer thread.
* @param line the line.
*/
void handle(String line); /**
* Handles an Exception .
* <p>
* <b>Note:</b> this is called from the tailer thread.
* @param ex the exception.
*/
void handle(Exception ex);
}
接口实现
package com.snow.tailer; public class TailerListenerAdapter implements TailerListener {
/**
* The tailer will call this method during construction,
* giving the listener a method of stopping the tailer.
* @param tailer the tailer.
*/
public void init(Tailer tailer) {
} /**
* This method is called if the tailed file is not found.
*/
public void fileNotFound() {
} /**
* Called if a file rotation is detected.
*
* This method is called before the file is reopened, and fileNotFound may
* be called if the new file has not yet been created.
*/
public void fileRotated() {
} /**
* Handles a line from a Tailer.
* @param line the line.
*/
public void handle(String line) {
} /**
* Handles an Exception .
* @param ex the exception.
*/
public void handle(Exception ex) {
} }
定义Tailer.java
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.snow.tailer; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile; /**
* Simple implementation of the unix "tail -f" functionality.
* <p>
* <h2>1. Create a TailerListener implementation</h3>
* <p>
* First you need to create a {@link TailerListener} implementation
* ({@link TailerListenerAdapter} is provided for convenience so that you don't have to
* implement every method).
* </p>
*
* <p>For example:</p>
* <pre>
* public class MyTailerListener extends TailerListenerAdapter {
* public void handle(String line) {
* System.out.println(line);
* }
* }
* </pre>
*
* <h2>2. Using a Tailer</h2>
*
* You can create and use a Tailer in one of three ways:
* <ul>
* <li>Using one of the static helper methods:
* <ul>
* <li>{@link Tailer#create(File, TailerListener)}</li>
* <li>{@link Tailer#create(File, TailerListener, long)}</li>
* <li>{@link Tailer#create(File, TailerListener, long, boolean)}</li>
* </ul>
* </li>
* <li>Using an {@link java.util.concurrent.Executor}</li>
* <li>Using an {@link Thread}</li>
* </ul>
*
* An example of each of these is shown below.
*
* <h3>2.1 Using the static helper method</h3>
*
* <pre>
* TailerListener listener = new MyTailerListener();
* Tailer tailer = Tailer.create(file, listener, delay);
* </pre>
*
* <h3>2.2 Use an Executor</h3>
*
* <pre>
* TailerListener listener = new MyTailerListener();
* Tailer tailer = new Tailer(file, listener, delay);
*
* // stupid executor impl. for demo purposes
* Executor executor = new Executor() {
* public void execute(Runnable command) {
* command.run();
* }
* };
*
* executor.execute(tailer);
* </pre>
*
*
* <h3>2.3 Use a Thread</h3>
* <pre>
* TailerListener listener = new MyTailerListener();
* Tailer tailer = new Tailer(file, listener, delay);
* Thread thread = new Thread(tailer);
* thread.setDaemon(true); // optional
* thread.start();
* </pre>
*
* <h2>3. Stop Tailing</h3>
* <p>Remember to stop the tailer when you have done with it:</p>
* <pre>
* tailer.stop();
* </pre>
*
* @see TailerListener
* @see TailerListenerAdapter
* @version $Id: Tailer.java 1348698 2012-06-11 01:09:58Z ggregory $
* @since 2.0
*/
public class Tailer implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(Tailer.class); private static final int DEFAULT_DELAY_MILLIS = 1000; private static final String RAF_MODE = "r"; private static final int DEFAULT_BUFSIZE = 4096; /**
* Buffer on top of RandomAccessFile.
*/
private final byte inbuf[]; /**
* The file which will be tailed.
*/
private final File file; /**
* The amount of time to wait for the file to be updated.
*/
private final long delayMillis; /**
* Whether to tail from the end or start of file
*/
private final boolean end; /**
* The listener to notify of events when tailing.
*/
private final TailerListener listener; /**
* Whether to close and reopen the file whilst waiting for more input.
*/
private final boolean reOpen; /**
* The tailer will run as long as this value is true.
*/
private volatile boolean run = true;
private volatile boolean resetFilePositionIfOverwrittenWithTheSameLength = false; /**
* Creates a Tailer for the given file, starting from the beginning, with the default delay of 1.0s.
* @param file The file to follow.
* @param listener the TailerListener to use.
*/
public Tailer(File file, TailerListener listener) {
this(file, listener, DEFAULT_DELAY_MILLIS);
} /**
* Creates a Tailer for the given file, starting from the beginning.
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
*/
public Tailer(File file, TailerListener listener, long delayMillis) {
this(file, listener, delayMillis, false);
} /**
* Creates a Tailer for the given file, with a delay other than the default 1.0s.
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
*/
public Tailer(File file, TailerListener listener, long delayMillis, boolean end) {
this(file, listener, delayMillis, end, DEFAULT_BUFSIZE);
logger.info("Tailer inited from customer class.");
} /**
* Creates a Tailer for the given file, with a delay other than the default 1.0s.
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param reOpen if true, close and reopen the file between reading chunks
*/
public Tailer(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen) {
this(file, listener, delayMillis, end, reOpen, DEFAULT_BUFSIZE);
} /**
* Creates a Tailer for the given file, with a specified buffer size.
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param bufSize Buffer size
*/
public Tailer(File file, TailerListener listener, long delayMillis, boolean end, int bufSize) {
this(file, listener, delayMillis, end, false, bufSize);
} /**
* Creates a Tailer for the given file, with a specified buffer size.
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param reOpen if true, close and reopen the file between reading chunks
* @param bufSize Buffer size
*/
public Tailer(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize) {
this.file = file;
this.delayMillis = delayMillis;
this.end = end; this.inbuf = new byte[bufSize]; // Save and prepare the listener
this.listener = listener;
listener.init(this);
this.reOpen = reOpen;
} /**
* Creates and starts a Tailer for the given file.
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param bufSize buffer size.
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, int bufSize) {
Tailer tailer = new Tailer(file, listener, delayMillis, end, bufSize);
Thread thread = new Thread(tailer);
thread.setDaemon(true);
thread.start();
return tailer;
} /**
* Creates and starts a Tailer for the given file.
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param reOpen whether to close/reopen the file between chunks
* @param bufSize buffer size.
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize) {
Tailer tailer = new Tailer(file, listener, delayMillis, end, reOpen, bufSize);
Thread thread = new Thread(tailer);
thread.setDaemon(true);
thread.start();
return tailer;
} /**
* Creates and starts a Tailer for the given file with default buffer size.
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end) {
return create(file, listener, delayMillis, end, DEFAULT_BUFSIZE);
} /**
* Creates and starts a Tailer for the given file with default buffer size.
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
* @param reOpen whether to close/reopen the file between chunks
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen) {
return create(file, listener, delayMillis, end, reOpen, DEFAULT_BUFSIZE);
} /**
* Creates and starts a Tailer for the given file, starting at the beginning of the file
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @param delayMillis the delay between checks of the file for new content in milliseconds.
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener, long delayMillis) {
return create(file, listener, delayMillis, false);
} /**
* Creates and starts a Tailer for the given file, starting at the beginning of the file
* with the default delay of 1.0s
*
* @param file the file to follow.
* @param listener the TailerListener to use.
* @return The new tailer
*/
public static Tailer create(File file, TailerListener listener) {
return create(file, listener, DEFAULT_DELAY_MILLIS, false);
} /**
* Return the file.
*
* @return the file
*/
public File getFile() {
return file;
} /**
* Return the delay in milliseconds.
*
* @return the delay in milliseconds.
*/
public long getDelay() {
return delayMillis;
} /**
* Follows changes in the file, calling the TailerListener's handle method for each new line.
*/
@Override
public void run() {
RandomAccessFile reader = null;
try {
long last = 0; // The last time the file was checked for changes
long position = 0; // position within the file
// Open the file
while (run && reader == null) {
try {
reader = new RandomAccessFile(file, RAF_MODE);
} catch (FileNotFoundException e) {
listener.fileNotFound();
} if (reader == null) {
try {
Thread.sleep(delayMillis);
} catch (InterruptedException e) {
}
} else {
// The current position in the file
position = end ? file.length() : 0;
last = file.lastModified();
reader.seek(position);
}
} while (run) { boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first // Check the file length to see if it was rotated
long length = file.length(); if (length < position) {
logger.info(String.format("rotated, legth=%s, position=%s", length, position));
// File was rotated
listener.fileRotated(); // Reopen the reader after rotation
try {
// Ensure that the old file is closed iff we re-open it successfully
RandomAccessFile save = reader;
reader = new RandomAccessFile(file, RAF_MODE);
position = 0;
// close old file explicitly rather than relying on GC picking up previous
// RAF
IOUtils.closeQuietly(save);
} catch (FileNotFoundException e) {
// in this case we continue to use the previous reader and position values
listener.fileNotFound();
}
continue;
} else { // File was not rotated // See if the file needs to be read again
if (length > position) { // The file has more content than it did last time
position = readLines(reader);
last = file.lastModified(); } else if (newer) {
logger.info(String.format("newer, legth=%s, position=%s", length, position));
if (resetFilePositionIfOverwrittenWithTheSameLength) {
/*
* This can happen if the file is truncated or overwritten with the exact same length of
* information. In cases like this, the file position needs to be reset
*/
position = 0;
reader.seek(position); // cannot be null here // Now we can read new lines
position = readLines(reader);
}
last = file.lastModified();
}
}
if (reOpen) {
IOUtils.closeQuietly(reader);
}
try {
Thread.sleep(delayMillis);
} catch (InterruptedException e) {
}
if (run && reOpen) {
reader = new RandomAccessFile(file, RAF_MODE);
reader.seek(position);
logger.info(String.format("reopen, legth=%s, position=%s", length, position));
}
} } catch (Exception e) { listener.handle(e); } finally {
IOUtils.closeQuietly(reader);
}
} /**
* Allows the tailer to complete its current loop and return.
*/
public void stop() {
this.run = false;
} /**
* Read new lines.
*
* @param reader The file to read
* @return The new position after the lines have been read
* @throws IOException if an I/O error occurs.
*/
private long readLines(RandomAccessFile reader) throws IOException {
StringBuilder sb = new StringBuilder(); long pos = reader.getFilePointer();
long rePos = pos; // position to re-read int num;
boolean seenCR = false;
while (run && ((num = reader.read(inbuf)) != -1)) {
for (int i = 0; i < num; i++) {
byte ch = inbuf[i];
switch (ch) {
case '\n':
seenCR = false; // swallow CR before LF
listener.handle(sb.toString());
sb.setLength(0);
rePos = pos + i + 1;
break;
case '\r':
if (seenCR) {
sb.append('\r');
}
seenCR = true;
break;
default:
if (seenCR) {
seenCR = false; // swallow final CR
listener.handle(sb.toString());
sb.setLength(0);
rePos = pos + i + 1;
}
sb.append((char) ch); // add character, not its ascii value
}
} pos = reader.getFilePointer();
} reader.seek(rePos); // Ensure we can re-read if necessary
return rePos;
} }
封装使用函数:
/**
* @param inputFile 监控文件
* @param sleepInterval 当文件没有日志时sleep间隔
*/
private static void monitor(String inputFile, int sleepInterval) {
TailerListener listener = new TailerListenerAdapter() {
@Override
public void handle(String line) {
if (++count % 100000 == 0) {
log.info("{} lines sent since the program up.", count);
}
if (StringUtils.isEmpty(line)) {
log.warn("should not read empty line.");
return;
} else {
// do something ...
}
}
};
Tailer tailer = new Tailer(new File(inputFile), listener, sleepInterval, true);
tailer.run();
}
调用该函数即可。
JAVA 实现tail -f 日志文件监控功能的更多相关文章
- Linux下日志文件监控系统Logwatch的使用记录
Linux下日志文件监控系统Logwatch的使用记录 原文:http://www.cnblogs.com/kevingrace/p/6519504.html 在维护Linux服务器时,经常需要查看系 ...
- C#使用FileSystemWatcher控件实现的文件监控功能示例
本文实例讲述了C#使用FileSystemWatcher控件实现的文件监控功能.分享给大家供大家参考,具体如下: FileSystemWatcher 可以使用FileSystemWatcher组件监视 ...
- 四步搞定Zabbix 日志文件监控
Zabbix 日志文件监控 一.给运行Zabbix agent的用户授予要监控日志的读取权限. 1. 執行下面的命令,追加app的可讀權限: setfacl -m u:app:r-- /var/log ...
- tail -f 实时查看日志文件 linux查看日志后100行
tail -f 实时查看日志文件 tail -f 日志文件logtail - 100f 实时查看日志文件 后一百行tail -f -n 100 catalina.out linux查看日志后100行搜 ...
- 使用tail命令实时查看日志文件
[Shell] 纯文本查看 复制代码 ? 1 tail -f /日志文件 好了.就这样用.简单吧 退出ctrl+C
- Linux 系统中如何查看日志 (常用命令) tail -f
Linux 系统中如何查看日志 (常用命令) tail -f 日志文件 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日 ...
- java实现文件监控
文件监控器: package testfile; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; import ...
- 关于linux的一点好奇心(四):tail -f文件跟踪实现
关于文件跟踪,我们有很多的实际场景,比如查看某个系统日志的输出,当有变化时立即体现,以便进行问题排查:比如查看文件结尾的内容是啥,总之是刚需了. 1. 自己实现的文件跟踪 我们平时做功能开发时,也会遇 ...
- tail -f 在对文件进行动态追踪时失效的问题
在我是用 tail -f file.txt 对这个文件进行动态追踪时: 我重新打开一个新的终端进行vim编辑这个文件并且保存 这是我们发现,tail -f file.txt'动态追踪的这个文件没有任何 ...
随机推荐
- Android 个推 踩坑小结
公司一个项目之前在手机上一直可以正常运行,后来在平板上运行了一下,在欢迎页面卡出了,一直没有反应. 于是我就将项目在电脑上用Android Studio往平板上运行了一遍,看了下打印的Log日志,发现 ...
- 常用接口简析3---IList和List的解析
常用接口的解析(链接) 1.IEnumerable深入解析 2.IEnumerable.IEnumerator接口解析 3.IComparable.IComparable接口解析 学习第一步,先上菜: ...
- 分水岭 golang入坑系列
第三式开篇语有些负面, 所以这里就不贴了.有兴趣的自己可以去看看 https://andy-zhangtao.gitbooks.io/golang/content/ .怒发冲冠,意气之作.看完就完了, ...
- java二进制相关基础
转载请注明原创出处,谢谢! 说在前面 之前在JVM菜鸟进阶高手之路十(基础知识开场白)的时候简单提到了二进制相关问题,最近在看RocketMQ的源码的时候,发现涉及二进制的内容蛮多,jdk源码里面也是 ...
- idea—— 模版配置
修改File Header: /** * @author: ${USER} Date: ${DATE} Time: ${TIME} */ 新增Package Header: /* * Copyrigh ...
- PHP招聘那些事,公司真的不需要培训班出来的人么?
就业形势严峻的情况下,每个企业对于人才的需求都不一样,并不是说公司不愿意招聘培训班出来的人,而是看你的能力是不是能胜任企业招聘人才的需求,是不是能给企业带来价值的人. 现在市面上的培训机构多如牛毛,然 ...
- hdu 1496 Equations hash表
hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...
- PHP7类型约束
在PHP7之前,函数和类方法不需要声明变量类型,任何数据都可以被传递和返回,导致几乎大部分的调用操作都要判断返回的数据类型是否合格. 为了解决这个问题,PHP7引入了类型声明. 目前有两类变量可以声明 ...
- 学习python登录demo
要求编写登录接口 : 1. 输入用户名和密码 2.认证成功后显示欢迎信息 3.用户名输错,提示用户不存在,重新输入(5次错误,提示尝试次数过多,退出程序) 4.用户名正确,密码错误,提示密码错误,重新 ...
- springmvc关于前台日期作为实体类对象参数类型转换错误
页面报错: 后台错误: Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [ty ...