in java TailListener,how to avoid duplicate log messages

*爱你&永不变心* 提交于 2019-12-02 02:15:45

问题


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.


回答1:


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.




回答2:


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.




回答3:


Following code removed the issue with two invocations of the TailerListenerAdapter:handle function.

TailerListener listener = new TailerListener(topic);
Tailer tailer = new Tailer(new File(path), listener, sleep, true);
Thread thread = new Thread(tailer);
thread.setDaemon(true);
thread.start(); 


来源:https://stackoverflow.com/questions/5921394/in-java-taillistener-how-to-avoid-duplicate-log-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!