[logback-user] Custom TextAreaAppender

ceki ceki at qos.ch
Tue Nov 6 13:12:22 CET 2012


Hi Maurizio,

Logback appenders are very similar to log4j appenders. One major 
difference is that they need to be started (by calling the start() 
method) to become active.

On 06.11.2012 10:55, m.lippa at ads.it wrote:
> Hi all,
> I'm switching my first java desktop application from log4j to logback,
> mainly because we neede a reliable DailyRollingFileAppender, and we were
> told the log4j implementation is not.
> So far all ok, we simply changed the Logger declaration and all worked well.
> The problem is our custom TextAreaAppender we are currently using to log
> the events to a text area in the application. The log4j implementation
> is very simple:
>
> public class TextAreaAppender extends WriterAppender {
>
>      static private JTextArea jTextArea = null;
>      public static void setTextArea(JTextArea jTextArea) {
>          TextAreaAppender.jTextArea = jTextArea;
>      }
>
>      /**
>       * Format and then append the loggingEvent to the stored JTextArea.
>       */
>      public void append(LoggingEvent loggingEvent) {
>          if (layout != null) {
> final String message = layout.format(loggingEvent);
> StringBuilder sb = new StringBuilder();
> if (loggingEvent.getThrowableInformation() != null) {
>      String[] stackTrace = loggingEvent.getThrowableStrRep();
>      for (String s : stackTrace) {
>          sb.append(s);
>          sb.append(Layout.LINE_SEP);
>      }
> }
> final String stackTrace = sb.toString();
>
> SwingUtilities.invokeLater(new Runnable() {
>      public void run() {
>          if (jTextArea != null) {
>              jTextArea.append(message);
>              if (!stackTrace.isEmpty()) {
>                  jTextArea.append(stackTrace);
>              }
>          }
>      }
> });
>          }
>      }
> }
>
> I worked a couple of day but did not manage to get a logback version of
> this appender to work. Do you have any sample code available?
>
> thanks in advance
> Maurizio


-- 
Ceki
65% of statistics are made up on the spot


More information about the Logback-user mailing list