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;
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;
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)
{
}
/**
- * 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)
{
}
/**
- * 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
*/
}
/**
- * 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)
}
/**
- * 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)
{
* 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)
*/
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.");
} 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);
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.");
} 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);
* 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)
{
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;
/**
{
if (Cache.log != null)
{
- Cache.log.setLevel((Level) logLevelCombo.getSelectedItem());
+ Log4j.setLevel(Cache.log,
+ (Level) logLevelCombo.getSelectedItem());
}
}
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;
// 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()
// reset log level to what it was before
if (Cache.log != null)
{
- Cache.log.setLevel(startingLogLevel);
+ Log4j.setLevel(Cache.log, startingLogLevel);
}
unredirectStreams();
*/
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. */
/**
* 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()
*/
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;
/**
* 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
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);
/**
*/
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
--- /dev/null
+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);
+ }
+}
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;
/**
*
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;
} catch (Exception e)
{
String msg = "I18N missing: " + loc + "\t" + key;
- logWarning(key, msg);
+ logWarning(key, msg);
}
return value;
}
*/
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;
}
* @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);
+ }
}
}