我能够使用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);
}
}
请告知。
答案 0 :(得分:1)
一种方法是设置线程名称
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);
}
}