gpt4 book ai didi

java - 如何在使用 Apache 的 Tailer 包监视文件时获取文件名?

转载 作者:行者123 更新时间:2023-12-02 11:23:51 25 4
gpt4 key购买 nike

我可以使用 org.apache.commons.io.input.Tailer 包来跟踪多个日志文件,但无法获取它们的名称。可用的监听器(TailerListenerAdapter)不提供任何方法来获取事件发生的文件的名称。

public class ApacheLogFileTailer {
public static void main(String[] args) {

String filelog1 = "C:\\Users\\eventfile1.log";
String filelog2 = "C:\\Users\\eventfile2.log";

ApacheLogFileTailerListener listener = new ApacheLogFileTailerListener();

Tailer tailer1 = new Tailer(new File(filelog1), listener, 50);
Thread tailerThread1 =new Thread(tailer1);
tailerThread1.start();

Tailer tailer2 = new Tailer(new File(filelog2), listener, 50);
Thread tailerThread2 =new Thread(tailer2);
tailerThread2.start();

}
}

public class ApacheLogFileTailerListener extends TailerListenerAdapter {

@Override
public void handle(String line) {
// How to get the name of the file changed ??
System.out.println("Log file change "+line);
super.handle(line);
}

}

请指教。

最佳答案

一种方法是设置线程名称

    public class ApacheLogFileTailer {
public static void main(String[] args) {

String filelog1 = "C:\\Users\\eventfile1.log";
String filelog2 = "C:\\Users\\eventfile2.log";

ApacheLogFileTailerListener listener = new ApacheLogFileTailerListener();

Tailer tailer1 = new Tailer(new File(filelog1), listener, 50);
Thread tailerThread1 =new Thread(tailer1);
tailerThread1.setName("eventfile1.log");
tailerThread1.start();

Tailer tailer2 = new Tailer(new File(filelog2), listener, 50);
Thread tailerThread2 =new Thread(tailer2);
tailerThread1.setName("eventfile2.log");
tailerThread2.start();

}
}

public class ApacheLogFileTailerListener extends TailerListenerAdapter {

@Override
public void handle(String line) {
// How to get the name of the file changed ??
System.out.println("Log file change "+line);
System.out.println("Log file change name "+Thread.currentThread().getName());
super.handle(line);
}

}

关于java - 如何在使用 Apache 的 Tailer 包监视文件时获取文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49735961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com