JAL-3949 An attempt at converting to Log4j 2 -- no output achieved!
authorBen Soares <b.soares@dundee.ac.uk>
Fri, 4 Feb 2022 18:08:09 +0000 (18:08 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Fri, 4 Feb 2022 18:08:09 +0000 (18:08 +0000)
12 files changed:
j11lib/log4j-1.2-api-2.17.1.jar [deleted file]
j11lib/slf4j-log4j12-1.7.32.jar [deleted file]
j8lib/log4j-1.2-api-2.17.1.jar [deleted file]
j8lib/slf4j-log4j12-1.7.32.jar [deleted file]
src/jalview/bin/Cache.java
src/jalview/gui/Console.java
src/jalview/gui/JalviewAppender.java
src/jalview/io/vamsas/DatastoreItem.java
src/jalview/io/vamsas/DatastoreRegistry.java
src/jalview/javascript/log4j/Layout.java
src/jalview/util/Log4j.java [new file with mode: 0644]
src/jalview/util/MessageManager.java

diff --git a/j11lib/log4j-1.2-api-2.17.1.jar b/j11lib/log4j-1.2-api-2.17.1.jar
deleted file mode 100644 (file)
index ef4f08d..0000000
Binary files a/j11lib/log4j-1.2-api-2.17.1.jar and /dev/null differ
diff --git a/j11lib/slf4j-log4j12-1.7.32.jar b/j11lib/slf4j-log4j12-1.7.32.jar
deleted file mode 100644 (file)
index 4b0e8b7..0000000
Binary files a/j11lib/slf4j-log4j12-1.7.32.jar and /dev/null differ
diff --git a/j8lib/log4j-1.2-api-2.17.1.jar b/j8lib/log4j-1.2-api-2.17.1.jar
deleted file mode 100644 (file)
index ef4f08d..0000000
Binary files a/j8lib/log4j-1.2-api-2.17.1.jar and /dev/null differ
diff --git a/j8lib/slf4j-log4j12-1.7.32.jar b/j8lib/slf4j-log4j12-1.7.32.jar
deleted file mode 100644 (file)
index 4b0e8b7..0000000
Binary files a/j8lib/slf4j-log4j12-1.7.32.jar and /dev/null differ
index bde6362..250f378 100755 (executable)
@@ -47,11 +47,9 @@ import java.util.regex.Pattern;
 import javax.swing.LookAndFeel;
 import javax.swing.UIManager;
 
-import org.apache.log4j.ConsoleAppender;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-import org.apache.log4j.SimpleLayout;
-import org.apache.log4j.builders.appender.ConsoleAppenderBuilder;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
@@ -68,6 +66,7 @@ import jalview.structure.StructureImportSettings;
 import jalview.urls.IdOrgSettings;
 import jalview.util.ChannelProperties;
 import jalview.util.ColorUtils;
+import jalview.util.Log4j;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.sifts.SiftsSettings;
@@ -335,30 +334,31 @@ public class Cache
     try
     {
       // configure the root logger to stderr
-      ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
-      AppenderComponentBuilder consoleApp = builder.newAppender("stderr", "Console");
+      ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory
+              .newConfigurationBuilder();
+      AppenderComponentBuilder consoleApp = builder.newAppender("stderr",
+              "Console");
       builder.add(consoleApp);
       Configurator.initialize(builder.build());
       // log output
-      Logger laxis = Logger.getLogger("org.apache.axis");
-      jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
+      Logger laxis = LogManager.getLogger("org.apache.axis");
+      log = LogManager.getLogger("jalview.bin.Jalview");
 
-      laxis.setLevel(Level.toLevel(
+      Log4j.setLevel(laxis, Level.toLevel(
               Cache.getDefault("logs.Axis.Level", Level.INFO.toString())));
       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
       // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
       // Level.INFO.toString())));
       // we shouldn't need to do this
-      org.apache.log4j.Logger.getRootLogger()
-              .setLevel(org.apache.log4j.Level.INFO);
+      Log4j.setRootLevel(Level.INFO);
 
-      jalview.bin.Cache.log.setLevel(Level.toLevel(Cache
+      Log4j.setLevel(log, Level.toLevel(Cache
               .getDefault("logs.Jalview.level", Level.INFO.toString())));
       // laxis.addAppender(ap);
       // lcastor.addAppender(ap);
       // jalview.bin.Cache.log.addAppender(ap);
       // Tell the user that debug is enabled
-      jalview.bin.Cache.log.debug(ChannelProperties.getProperty("app_name")
+      log.debug(ChannelProperties.getProperty("app_name")
               + " Debugging Output Follows.");
     } catch (Exception ex)
     {
@@ -368,8 +368,8 @@ public class Cache
   }
 
   /**
-   * Loads properties from the given properties file. Any existing properties are
-   * first cleared.
+   * Loads properties from the given properties file. Any existing properties
+   * are first cleared.
    */
   public static void loadProperties(String propsFile)
   {
@@ -708,10 +708,11 @@ public class Cache
   }
 
   /**
-   * Gets Jalview application property of given key. Returns null if key not found
+   * Gets Jalview application property of given key. Returns null if key not
+   * found
    * 
    * @param key
-   *              Name of property
+   *          Name of property
    * 
    * @return Property value
    */
@@ -727,8 +728,8 @@ public class Cache
   }
 
   /**
-   * These methods are used when checking if the saved preference is different to
-   * the default setting
+   * These methods are used when checking if the saved preference is different
+   * to the default setting
    */
 
   public static boolean getDefault(String property, boolean def)
@@ -761,8 +762,8 @@ public class Cache
   }
 
   /**
-   * Answers the value of the given property, or the supplied default value if the
-   * property is not set
+   * Answers the value of the given property, or the supplied default value if
+   * the property is not set
    */
   public static String getDefault(String property, String def)
   {
@@ -774,9 +775,9 @@ public class Cache
    * Stores property in the file "HOME_DIR/.jalview_properties"
    * 
    * @param key
-   *              Name of object
+   *          Name of object
    * @param obj
-   *              String value of property
+   *          String value of property
    * 
    * @return previous value of property (or null)
    */
@@ -849,14 +850,14 @@ public class Cache
         if (jalview.jbgui.GDesktop.class.getClassLoader()
                 .loadClass("uk.ac.vamsas.client.VorbaId") != null)
         {
-          jalview.bin.Cache.log.debug(
+          log.debug(
                   "Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
           vamsasJarsArePresent = 1;
-          Logger lvclient = Logger.getLogger("uk.ac.vamsas");
-          lvclient.setLevel(Level.toLevel(Cache
+          Logger lvclient = LogManager.getLogger("uk.ac.vamsas");
+          Log4j.setLevel(lvclient, Level.toLevel(Cache
                   .getDefault("logs.Vamsas.Level", Level.INFO.toString())));
 
-          lvclient.addAppender(log.getAppender("JalviewLogger"));
+          Log4j.addAppender(lvclient, log, "JalviewLogger");
           // Tell the user that debug is enabled
           lvclient.debug(ChannelProperties.getProperty("app_name")
                   + " Vamsas Client Debugging Output Follows.");
@@ -864,7 +865,7 @@ public class Cache
       } catch (Exception e)
       {
         vamsasJarsArePresent = 0;
-        jalview.bin.Cache.log.debug("Vamsas Classes are not present");
+        log.debug("Vamsas Classes are not present");
       }
     }
     return (vamsasJarsArePresent > 0);
@@ -889,14 +890,14 @@ public class Cache
         if (Cache.class.getClassLoader()
                 .loadClass("groovy.lang.GroovyObject") != null)
         {
-          jalview.bin.Cache.log.debug(
+          log.debug(
                   "Found Groovy (groovy.lang.GroovyObject can be loaded)");
           groovyJarsArePresent = 1;
-          Logger lgclient = Logger.getLogger("groovy");
-          lgclient.setLevel(Level.toLevel(Cache
+          Logger lgclient = LogManager.getLogger("groovy");
+          Log4j.setLevel(lgclient, Level.toLevel(Cache
                   .getDefault("logs.Groovy.Level", Level.INFO.toString())));
 
-          lgclient.addAppender(log.getAppender("JalviewLogger"));
+          Log4j.addAppender(lgclient, log, "JalviewLogger");
           // Tell the user that debug is enabled
           lgclient.debug(ChannelProperties.getProperty("app_name")
                   + " Groovy Client Debugging Output Follows.");
@@ -904,11 +905,11 @@ public class Cache
       } catch (Error e)
       {
         groovyJarsArePresent = 0;
-        jalview.bin.Cache.log.debug("Groovy Classes are not present", e);
+        log.debug("Groovy Classes are not present", e);
       } catch (Exception e)
       {
         groovyJarsArePresent = 0;
-        jalview.bin.Cache.log.debug("Groovy Classes are not present");
+        log.debug("Groovy Classes are not present");
       }
     }
     return (groovyJarsArePresent > 0);
@@ -1164,7 +1165,7 @@ public class Cache
    * Loads in user colour schemes from files.
    * 
    * @param files
-   *                a '|'-delimited list of file paths
+   *          a '|'-delimited list of file paths
    */
   public static void initUserColourSchemes(String files)
   {
index 050523e..1786c0c 100644 (file)
@@ -50,11 +50,11 @@ import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 
-import org.apache.log4j.Level;
-import org.apache.log4j.SimpleLayout;
+import org.apache.logging.log4j.Level;
 
 import jalview.bin.Cache;
 import jalview.util.ChannelProperties;
+import jalview.util.Log4j;
 import jalview.util.MessageManager;
 
 /**
@@ -186,7 +186,8 @@ public class Console extends WindowAdapter
       {
         if (Cache.log != null)
         {
-          Cache.log.setLevel((Level) logLevelCombo.getSelectedItem());
+          Log4j.setLevel(Cache.log,
+                  (Level) logLevelCombo.getSelectedItem());
         }
       }
 
@@ -279,7 +280,7 @@ public class Console extends WindowAdapter
         for (int i = 0; i < logLevelCombo.getItemCount(); i++)
         {
           Level l = (Level) logLevelCombo.getItemAt(i);
-          if (l.isGreaterOrEqual(setLogLevel))
+          if (l.isMoreSpecificThan(setLogLevel))
           {
             logLevelCombo.insertItemAt(setLogLevel, i);
             added = true;
@@ -472,9 +473,8 @@ public class Console extends WindowAdapter
     // desktop.add(frame);
     initConsole(false);
     JalviewAppender jappender = new JalviewAppender();
-    jappender.setLayout(new SimpleLayout());
     JalviewAppender.setTextArea(textArea);
-    org.apache.log4j.Logger.getRootLogger().addAppender(jappender);
+    Log4j.addAppenderToRootLogger(jappender);
   }
 
   public synchronized void stopConsole()
@@ -817,7 +817,7 @@ public class Console extends WindowAdapter
       // reset log level to what it was before
       if (Cache.log != null)
       {
-        Cache.log.setLevel(startingLogLevel);
+        Log4j.setLevel(Cache.log, startingLogLevel);
       }
 
       unredirectStreams();
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()
index 56d9fa4..a1d8380 100644 (file)
  */
 package jalview.io.vamsas;
 
-import jalview.bin.Cache;
-import jalview.io.VamsasAppDatastore;
-import jalview.util.MessageManager;
-
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
 import java.util.Vector;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import jalview.bin.Cache;
+import jalview.io.VamsasAppDatastore;
+import jalview.util.MessageManager;
 import uk.ac.vamsas.client.IClientDocument;
 import uk.ac.vamsas.client.Vobject;
 import uk.ac.vamsas.client.VorbaId;
@@ -60,8 +62,7 @@ public abstract class DatastoreItem
   /**
    * shared log instance
    */
-  protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
-          .getLogger(DatastoreItem.class);
+  protected static Logger log = LogManager.getLogger(DatastoreItem.class);
 
   /**
    * note: this is taken verbatim from jalview.io.VamsasAppDatastore
index b53de08..37026ba 100644 (file)
@@ -24,9 +24,12 @@ import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 public class DatastoreRegistry implements AutoCloseable
 {
-  protected static org.apache.log4j.Logger log = org.apache.log4j.Logger
+  protected static Logger log = LogManager
           .getLogger(DatastoreRegistry.class);
 
   /**
index e467e36..46c05d1 100644 (file)
  */
 package jalview.javascript.log4j;
 
+import org.apache.logging.log4j.core.layout.PatternLayout;
+
 import jalview.javascript.log4j.spi.LoggingEvent;
 import jalview.javascript.log4j.spi.OptionHandler;
-
-import org.apache.log4j.PatternLayout;
 //import org.apache.log4j.TTCCLayout;
 
 public abstract class Layout implements OptionHandler
diff --git a/src/jalview/util/Log4j.java b/src/jalview/util/Log4j.java
new file mode 100644 (file)
index 0000000..e6c660b
--- /dev/null
@@ -0,0 +1,52 @@
+package jalview.util;
+
+import java.util.Map;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+
+public class Log4j
+{
+  public static void setLevel(Logger logger, Level level)
+  {
+    LoggerContext context = (LoggerContext) LogManager.getContext(false);
+    Configuration config = context.getConfiguration();
+    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
+    loggerConfig.setLevel(level);
+  }
+
+  public static void setRootLevel(Level level)
+  {
+    setLevel(LogManager.getRootLogger(), level);
+  }
+
+  public static void addAppender(Logger logger, Logger logger2,
+          String name2)
+  {
+    LoggerContext context = (LoggerContext) LogManager.getContext(false);
+    Configuration config = context.getConfiguration();
+    LoggerConfig logger2Config = config.getLoggerConfig(logger2.getName());
+    Map<String, Appender> logger2AppendersMap = logger2Config
+            .getAppenders();
+    Appender appender = logger2AppendersMap.get(name2);
+    addAppender(logger, appender);
+  }
+
+  public static void addAppender(Logger logger, Appender appender)
+  {
+    LoggerContext context = (LoggerContext) LogManager.getContext(false);
+    Configuration config = context.getConfiguration();
+    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
+    loggerConfig.addAppender(appender, null, null);
+  }
+
+  public static void addAppenderToRootLogger(Appender appender)
+  {
+    Log4j.addAppender(LogManager.getRootLogger(), appender);
+  }
+}
index ba7da10..c562d8d 100644 (file)
@@ -27,7 +27,8 @@ import java.util.ResourceBundle;
 import java.util.ResourceBundle.Control;
 import java.util.Set;
 
-import org.apache.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 /**
  * 
@@ -41,7 +42,7 @@ public class MessageManager
   private static ResourceBundle rb;
 
   // BH 2018 switched to org.apache.llog4j.Logger
-  private static Logger log = Logger
+  private static Logger log = LogManager
           .getLogger(MessageManager.class.getCanonicalName());
 
   private static Locale loc;
@@ -94,7 +95,7 @@ public class MessageManager
     } catch (Exception e)
     {
       String msg = "I18N missing: " + loc + "\t" + key;
-         logWarning(key, msg);
+      logWarning(key, msg);
     }
     return value;
   }
@@ -162,15 +163,16 @@ public class MessageManager
    */
   public static String getStringOrReturn(String keyroot, String name)
   {
-    String smkey = keyroot + name.toLowerCase(Locale.ROOT).replaceAll(" ", "");
+    String smkey = keyroot
+            + name.toLowerCase(Locale.ROOT).replaceAll(" ", "");
     try
     {
       name = rb.getString(smkey);
     } catch (Exception x)
     {
-      String msg = "I18N missing key with root " + keyroot + ": " + loc + "\t"
-                         + smkey;
-         logWarning(smkey, msg);
+      String msg = "I18N missing key with root " + keyroot + ": " + loc
+              + "\t" + smkey;
+      logWarning(smkey, msg);
     }
     return name;
   }
@@ -181,12 +183,12 @@ public class MessageManager
    * @param key
    * @param msg
    */
-  private static void logWarning(String key, String msg) 
+  private static void logWarning(String key, String msg)
   {
-       if (!reportedMissing.contains(key))
-       {
+    if (!reportedMissing.contains(key))
+    {
       reportedMissing.add(key);
-         log.info(msg);
-       }
+      log.info(msg);
+    }
   }
 }