我需要破解一个小工具。它应该读取几个文件并转换它们。现在可以在我的 IDE 中运行了。对于用户,我想添加一个小的 UI,仅显示日志输出。
您知道用于 logback 的现成 Swing 附加程序吗?或者将 System.out 重定向到一个小 UI,只有一个文本字段和一个“关闭”按钮?
PS:我不是在寻找电锯、竖锯或莉莉丝。我想要在应用程序中显示日志消息。
I need to hack up a small tool. It should read a couple of files and convert them. Right now that works in my IDE. For the user, I'd like to add a small UI which simply shows the log output.
Do you know of a ready-to-use Swing appender for logback? Or something which redirects System.out to a little UI with nothing more than a text field and a "Close" button?
PS: I'm not looking for Chainsaw or Jigsaw or Lilith. I want the display of the log messages in the application, please.
发布评论
评论(3)
您需要编写一个自定义附加程序类,如下所示:
您可以将 EchoEncoder 替换为 PatternLayoutEncoder(请参阅 logback 示例文件夹中的 CountingConsoleAppender 示例)。
编码器会将每个事件写入字节缓冲区,然后您可以提取字符串并将其写入 JTextPane 或 JTextArea 或您想要的任何内容。
You need to write a custom appender class like so:
You can replace the EchoEncoder with a PatternLayoutEncoder (see CountingConsoleAppender example in the logback examples folder).
The encoder will write each event to a byte buffer, which you can then extract a string and write this to your JTextPane or JTextArea, or whatever you want.
我经常依赖
JTextArea#append()
,如本示例。与大多数 Swing 不同,该方法恰好是线程安全的。附录:
Console
是一个相关示例将 System.out 和 System.err 重定向到JTextArea
。I often rely on
JTextArea#append()
, as suggested in this example. Unlike most of Swing, the method happens to be thread safe.Addendum:
Console
is a related example that redirectsSystem.out
andSystem.err
to aJTextArea
.没有保证,但这是我刚刚写的示例:
No warranty, but here's a sample that I just wrote: