将记录器消息写入文件和文本区域,同时保持 Java 中的默认行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10785560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write logger message to file and textarea while maintaining default behaviour in Java
提问by Orion
I'm currently working on logging messages from my application in .log files. This is working fine so far but now I'm trying to output the same message to a textarea. I have been using the default logger for all this.
我目前正在将来自我的应用程序的消息记录在 .log 文件中。到目前为止,这工作正常,但现在我正在尝试将相同的消息输出到文本区域。我一直在使用默认记录器。
This so that I have one class that does all the work of logging to a .log file and writing the same output to a textarea for the admin to see.
这样我就有了一个类来完成记录到 .log 文件并将相同的输出写入 textarea 以供管理员查看的所有工作。
This is a Java swing JFrame application containing just a textarea (all I need). There is a bunch going on in the background and all of that activity has to be logged for review/debugging.
这是一个 Java Swing JFrame 应用程序,只包含一个 textarea(我需要的所有内容)。后台有很多事情要做,所有这些活动都必须记录下来以供/调试。
I've been having trouble finding a good example so I was wondering if you guys could help me.
我一直很难找到一个好的例子,所以我想知道你们是否可以帮助我。
回答by Edwin Dalorzo
In your case, since you are using JDK default logging, your option is to write your own java.util.Handler
and implement the publish method. Somewhat like this:
在您的情况下,由于您使用的是 JDK 默认日志记录,因此您可以选择自己编写java.util.Handler
并实现发布方法。有点像这样:
public class TextAreaHandler extends java.util.logging.Handler {
private JTextArea textArea = new JTextArea(50, 50);
@Override
public void publish(final LogRecord record) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
StringWriter text = new StringWriter();
PrintWriter out = new PrintWriter(text);
out.println(textArea.getText());
out.printf("[%s] [Thread-%d]: %s.%s -> %s", record.getLevel(),
record.getThreadID(), record.getSourceClassName(),
record.getSourceMethodName(), record.getMessage());
textArea.setText(text.toString());
}
});
}
public JTextArea getTextArea() {
return this.textArea;
}
//...
}
Then, you can get the text area from your handler in your Swing application, somewhat like:
然后,您可以从 Swing 应用程序中的处理程序获取文本区域,有点像:
for(Handler handler: logger.getHandlers()){
if(handler instanceof TextAreaHandler){
TextAreaHandler textAreaHandler = (TextAreaHandler) handler;
getContentPane().add(textAreaHandler.getTextArea());
}
}
Then, you make sure your logging.properties
file contains the configuration of your new handler:
然后,确保您的logging.properties
文件包含新处理程序的配置:
hackers.logging.TestDrive.level=INFO
hackers.logging.TestDrive.handlers=hackers.logging.TextAreaHandler
And, if you are not going to put this configuration in your default logging.properties
file (located in your JRE lib folder) then make sure to provide the path to your customized logging.properties
file in a property at application startup:
而且,如果您不打算将此配置放在默认logging.properties
文件中(位于 JRE lib 文件夹中),请确保logging.properties
在应用程序启动时在属性中提供自定义文件的路径:
java -Djava.util.logging.config.file=my-customized-logging.properties ...
回答by Thach Van
Just extends from StreamHandler - this is how ConsoleHandler and FileHandler works. And, override the publish
function:
只是从 StreamHandler 扩展 - 这就是 ConsoleHandler 和 FileHandler 的工作原理。并且,覆盖publish
函数:
public class TextAreaHandler extends StreamHandler {
JTextArea textArea = null;
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void publish(LogRecord record) {
super.publish(record);
flush();
if (textArea != null) {
textArea.appendText(getFormatter().format(record));
}
}
}
Set the output place first before logging message, for example:
在记录消息之前先设置输出位置,例如:
public TextAreaHandler textAreaHandler = new TextAreaHandler();
textAreaHandler.setTextArea(<your JTextArea control>);
回答by dbf
If it is a monolit application you can write log4j custom appender
that will call update of content of textarea and add new lines there.
If there are two independent applications (one that produces logs and one that displays them) you can set up a kind of connection between them (socket/messaging etc) to notify admin application about logged lines and update them.
如果它是一个单体应用程序,您可以编写log4j custom appender
它将调用 textarea 内容的更新并在那里添加新行。
如果有两个独立的应用程序(一个生成日志,一个显示它们),您可以在它们之间建立一种连接(套接字/消息传递等)以通知管理应用程序有关已记录的行并更新它们。