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.
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.
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.
I've been having trouble finding a good example so I was wondering if you guys could help me.
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:Then, you can get the text area from your handler in your Swing application, somewhat like:
Then, you make sure your
logging.properties
file contains the configuration of your new handler: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 customizedlogging.properties
file in a property at application startup: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.
Just extends from StreamHandler - this is how ConsoleHandler and FileHandler works. And, override the
publish
function:Set the output place first before logging message, for example: