public class TextAreaAppender extends WriterAppender {
private static volatile TextArea textArea = null;
private static final Logger log = LoggerFactory.getLogger(TextAreaAppender.class);
/**
* Set the target TextArea for the logging information to appear.
*
* @param textArea
*/
public static void setTextArea(final TextArea textArea) {
TextAreaAppender.textArea = textArea;
}
/**
* Format and then append the loggingEvent to the stored TextArea.
*
* @param loggingEvent
*/
@Override
public void append(final LoggingEvent loggingEvent) {
final String message = this.layout.format(loggingEvent);
// Append formatted message to text area using the Thread.
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
if (textArea != null) {
if (textArea.getText().length() == 0) {
Platform.runLater( () -> {
textArea.setText(message);
});
} else {
textArea.selectEnd();
Platform.runLater( () -> {
textArea.insertText(textArea.getText().length(), message);
});
}
}
} catch (final Throwable t) {
log.error("Unable to append log to text area: " + t.getMessage());
}
return null;
}
};
new Thread(task).start();
}
That's my class. got it from http://www.rshingleton.com/javafx-log4j-textarea-log-appender/ It writes the logs that also appear at console to a textarea. The Problem is that the logs appear in a different wrong order than in the console. why? thanks in advance