英文:
Print Log4j2 output into JTextArea
问题
我有一个在我的程序中的JTextArea
,我想用它来显示程序的日志。我查了一些资料,发现了Log4j2文档中的OutputStreamAppender
,我认为我可以使用它来将日志输出到一个OutputStream
,然后将其传递到JTextArea
中。不幸的是,关于这方面的文档很少(至少我找不到)。我这样做对吗?如果是的话,我该如何使用OutputStreamAppender
?如果不是,我应该做什么?我宁愿不创建自己的Appender。
谢谢
英文:
I have a JTextArea
in my program which I would like to display the program's logs. I've done some digging and found OutputStreamAppender
in the Log4j2 documentation, which I assume I could use to output logs to an OutputStream
which I could then pipe into the JTextArea
. Unfortunately there isn't much documentation (at least that I could find) on this. Am I on the right track here? If so, how do I use OutputStreamAppender
? If not, what should I be doing? I'd rather not create my own Appender.
Thanks
答案1
得分: 1
这是您提供的Java代码的翻译:
也许尝试一些类似的东西(快速且简单 - 您绝对需要防止无限循环):
import java.time.format.*;
import java.time.*;
import java.util.Comparator;
import java.nio.file.*;
import java.io.IOException;
public class FName {
public static void main(String[] args) {
try {
//System.out.println(FName.getLatestLogFileName(Path.of("/tmp")));
System.out.println(FName.findLatestLogFileName(Path.of("/tmp")));
} catch (Throwable t) {
t.printStackTrace();
}
}
public static Path findLatestLogFileName(Path logsDir) throws Exception {
// Tighten this pattern up as necessary
final String LOGFILE_PAT =".*\\.log";
return Files.find(logsDir, 1, (p, attr) ->p.getFileName().toString().matches(LOGFILE_PAT)).
sorted(Comparator.comparingLong(p ->p.toFile().lastModified())).
reduce((first, second) -> second).
orElse(null);
}
public static Path getLatestLogFileName(Path logsDir) {
Path result = null;
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM uuuu HH.mm.ss");
boolean foundLogFile = false;
while (result == null) {
Path test = logsDir.resolve(Path.of(dtf.format(ldt) + ".log"));
if (Files.exists(test)) {
result = test;
} else {
ldt = ldt.minusSeconds(1);
}
}
return result;
}
}
英文:
Maybe try something like (quick and dirty - you'd definitely want to guard against endless looping):
import java.time.format.*;
import java.time.*;
import java.util.Comparator;
import java.nio.file.*;
import java.io.IOException;
public class FName {
public static void main(String[] args) {
try {
//System.out.println(FName.getLatestLogFileName(Path.of("/tmp")));
System.out.println(FName.findLatestLogFileName(Path.of("/tmp")));
} catch (Throwable t) {
t.printStackTrace();
}
}
public static Path findLatestLogFileName(Path logsDir) throws Exception {
// Tighten this pattern up as necessary
final String LOGFILE_PAT =".*\\.log";
return Files.find(logsDir, 1, (p, attr) ->p.getFileName().toString().matches(LOGFILE_PAT)).
sorted(Comparator.comparingLong(p ->p.toFile().lastModified())).
reduce((first, second) -> second).
orElse(null);
}
public static Path getLatestLogFileName(Path logsDir) {
Path result = null;
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMMM uuuu HH.mm.ss");
boolean foundLogFile = false;
while (result == null) {
Path test = logsDir.resolve(Path.of(dtf.format(ldt) + ".log"));
if (Files.exists(test)) {
result = test;
} else {
ldt = ldt.minusSeconds(1);
}
}
return result;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论