我可以使用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);
}
}