在java的TailListener中,如何避免重复的日志消息 [英] in java TailListener,how to avoid duplicate log messages

查看:452
本文介绍了在java的TailListener中,如何避免重复的日志消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下。

  public static void main(String [] args){
// TODO代码应用程序逻辑在这里
文件pcounter_log =新文件(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);


$ / code $ / $ p

$ b我将监视日志文件。日志消息更新日志文件(c:\ development \temp\test.log),它会打印日志消息。

问题是,每当日志消息在日志文件中更新,它显示相同的日志消息两次,也有一些时间三次或四次,以避免这种重复的日志消息。

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

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

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.

解决方案

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.

这篇关于在java的TailListener中,如何避免重复的日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆