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.
No warranty, but here's a sample that I just wrote:
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.
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
.