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:
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:
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:
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:
java -Djava.util.logging.config.file=my-customized-logging.properties ...