将Log4j2的输出打印到JTextArea。

huangapple go评论75阅读模式
英文:

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;
}
}

huangapple
  • 本文由 发表于 2023年5月17日 22:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定