JAL-3949 An attempt at converting to Log4j 2 -- no output achieved!
[jalview.git] / src / jalview / gui / JalviewAppender.java
index 1d7064b..229a1f6 100644 (file)
  */
 package jalview.gui;
 
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+
 import javax.swing.JTextArea;
 import javax.swing.SwingUtilities;
 
-import org.apache.log4j.WriterAppender;
-import org.apache.log4j.spi.LoggingEvent;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.layout.PatternLayout;
 
 /**
  * From http://textareaappender.zcage.com/ the means to capture the logs, too.
  * Simple example of creating a Log4j appender that will write to a JTextArea.
  */
-public class JalviewAppender extends WriterAppender
+public class JalviewAppender extends AbstractAppender
 {
 
+  private final static Layout simpleLayout;
+
+  static
+  {
+    // SimpleLayout pattern found from
+    // https://logging.apache.org/log4j/log4j-2.12.4/manual/migration.html
+    simpleLayout = PatternLayout.newBuilder().withPattern("%level - %m%n")
+            .build();
+  }
+
+  protected JalviewAppender()
+  {
+    this("JalviewAppender", null, simpleLayout);
+  }
+
+  protected JalviewAppender(String name, Filter filter,
+          Layout<? extends Serializable> layout)
+  {
+    super(name, filter, layout);
+    // TODO Auto-generated constructor stub
+  }
+
   static private JTextArea jTextArea = null;
 
   /** Set the target JTextArea for the logging information to appear. */
@@ -44,9 +72,10 @@ public class JalviewAppender extends WriterAppender
   /**
    * Format and then append the loggingEvent to the stored JTextArea.
    */
-  public void append(LoggingEvent loggingEvent)
+  public void append(LogEvent logEvent)
   {
-    final String message = this.layout.format(loggingEvent);
+    final String message = new String(simpleLayout.toByteArray(logEvent),
+            StandardCharsets.UTF_8);
 
     // Append formatted message to textarea using the Swing Thread.
     SwingUtilities.invokeLater(new Runnable()