在java TailListener中,如何避免重复的日志消息

ssb*_*cse 3 java file-io apache-commons-io

我的代码如下.

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);
 }
}
Run Code Online (Sandbox Code Playgroud)

.ie,我正在监视日志文件.每当在日志文件(c:\ development\temp\test.log)中更新日志消息时,它将打印日志消息.

问题是,每当日志文件中的日志消息更新时,它会显示相同的日志消息两次,有时也会显示三到四次.以避免这些重复的日志消息.

小智 5

重复消息的原因之一是,如果您使用Tailer.create静态方法创建Tailer,它会自动启动监视日志的过程.

我们犯了一个错误:做一个tailer.run,它启动另一个监控实例并打印两次相同的条目.