在java TailListener中,如何避免重复的日志消息
我的代码如下。
public static void main(String[] args) {
// TODO code application logic here
File pcounter_log = new File("c:\development\temp\test.log");
try {
TailerListener listener = new PCTailListener();
Tailer tailer = new Tailer(pcounter_log, listener, 5000,true);
Thread thread = new Thread(tailer);
thread.start();
} catch (Exception e) {
System.out.println(e);
}
}
public class PCTailListener extends TailerListenerAdapter {
public void handle(String line) {
System.out.println(line);
}
}
.ie,我正在监视日志文件。每当日志文件(c:\development\temp\test.log)中更新日志消息时,它将打印日志消息。
问题是,每当日志文件中的日志消息更新时,它都会显示相同的日志消息两次,有时甚至显示三四次。如何避免这种重复的日志消息。
my code is given below .
public static void main(String[] args) {
// TODO code application logic here
File pcounter_log = new File("c:\development\temp\test.log");
try {
TailerListener listener = new PCTailListener();
Tailer tailer = new Tailer(pcounter_log, listener, 5000,true);
Thread thread = new Thread(tailer);
thread.start();
} catch (Exception e) {
System.out.println(e);
}
}
public class PCTailListener extends TailerListenerAdapter {
public void handle(String line) {
System.out.println(line);
}
}
.ie, i am monitoring the log file.whenever log messages updated in log file(c:\development\temp\test.log),it will print the log message.
The problem is,whenever log messages updated in log file,it displaying the same log message by two times and also some times by three or four times.how to avoid this duplicate log messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
出现重复消息的原因之一是,如果您使用 Tailer.create 静态方法创建 Tailer,它会自动启动监控日志的过程。
我们犯了一个错误,执行 tailer.run 启动另一个监视实例并打印相同的条目两次。
One of the reasons for the duplicate messages is that if you are using the Tailer.create static method to create the Tailer, it automatically starts the process of monitoring the log.
We make the mistake of doing a tailer.run which starts another monitoring instance and prints the same entries twice.
以下代码通过两次调用 TailerListenerAdapter:handle 函数消除了该问题。
Following code removed the issue with two invocations of the TailerListenerAdapter:handle function.
看看 Tailer 的代码,我不明白这是怎么发生的。您确定没有运行尾件的多个副本,并且日志文件中的消息实际上没有重复。
Looking at the code of Tailer, I can't see how that can happen. Are you sure that you aren't running multiple copies of the tailer, and that the messages aren't actually duplicated in the log file.