From 68b78c6fc657d3cd67694b55e877dfb868afa791 Mon Sep 17 00:00:00 2001 From: John Mayfield Date: Wed, 22 Dec 2021 10:27:46 +0000 Subject: [PATCH] Move over to Log4J2 configuration - allows us to remove some log4j-1.2 deps --- misc/log4j/pom.xml | 4 - .../openscience/cdk/tools/LoggingTool.java | 84 +++++++++++-------- .../cdk/config/data/log4j.properties | 10 --- .../org/openscience/cdk/tools/cdk-log4j2.xml | 12 +++ .../cdk/tools/LoggingToolTest.java | 4 +- 5 files changed, 65 insertions(+), 49 deletions(-) delete mode 100644 misc/log4j/src/main/resources/org/openscience/cdk/config/data/log4j.properties create mode 100644 misc/log4j/src/main/resources/org/openscience/cdk/tools/cdk-log4j2.xml diff --git a/misc/log4j/pom.xml b/misc/log4j/pom.xml index 71fbe3494d5..bed0c477792 100644 --- a/misc/log4j/pom.xml +++ b/misc/log4j/pom.xml @@ -14,10 +14,6 @@ cdk-log4j - - org.apache.logging.log4j - log4j-1.2-api - org.apache.logging.log4j log4j-core diff --git a/misc/log4j/src/main/java/org/openscience/cdk/tools/LoggingTool.java b/misc/log4j/src/main/java/org/openscience/cdk/tools/LoggingTool.java index 895ff8937f7..d4f55e0f183 100644 --- a/misc/log4j/src/main/java/org/openscience/cdk/tools/LoggingTool.java +++ b/misc/log4j/src/main/java/org/openscience/cdk/tools/LoggingTool.java @@ -19,12 +19,22 @@ */ package org.openscience.cdk.tools; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; +import org.apache.logging.log4j.internal.LogManagerStatus; + import java.io.BufferedReader; +import java.io.InputStream; import java.io.PrintWriter; import java.io.StringReader; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; /** * Useful for logging messages. Often used as a class static variable instantiated like: @@ -88,7 +98,7 @@ public class LoggingTool implements ILoggingTool { private boolean toSTDOUT = false; - private Logger log4jLogger; + private Logger log4jLogger; private static ILoggingTool logger; private String classname; @@ -97,6 +107,19 @@ public class LoggingTool implements ILoggingTool { /** Default number of StackTraceElements to be printed by debug(Exception). */ public final int DEFAULT_STACK_LENGTH = 5; + /** Log4J2 has customer levels and no longer has "TRACE_INT" etc so we can't know the values at compile + * time and therefore it's not possible use a switch. */ + private static final Map LOG4J2_LEVEL_TO_CDK_LEVEL = new HashMap<>(); + + static { + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.TRACE, TRACE); + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.DEBUG, DEBUG); + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.INFO, INFO); + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.WARN, WARN); + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.ERROR, ERROR); + LOG4J2_LEVEL_TO_CDK_LEVEL.put(Level.FATAL, FATAL); + } + /** * Constructs a LoggingTool which produces log lines without any special * indication which class the message originates from. @@ -126,7 +149,7 @@ public LoggingTool(Class classInst) { stackLength = DEFAULT_STACK_LENGTH; this.classname = classInst.getName(); try { - log4jLogger = Logger.getLogger(classname); + log4jLogger = LogManager.getLogger(classname); } catch (NoClassDefFoundError e) { toSTDOUT = true; logger.debug("Log4J class not found!"); @@ -149,9 +172,9 @@ public LoggingTool(Class classInst) { // by default debugging is set off, but it can be turned on // with starting java like "java -Dcdk.debugging=true" if (System.getProperty("cdk.debugging", "false").equals("true")) { - log4jLogger.setLevel(Level.DEBUG); + Configurator.setLevel(log4jLogger.getName(), Level.DEBUG); } else { - log4jLogger.setLevel(Level.WARN); + Configurator.setLevel(log4jLogger.getName(), Level.WARN); } if (System.getProperty("cdk.debug.stdout", "false").equals("true")) { toSTDOUT = true; @@ -170,13 +193,20 @@ public LoggingTool(Class classInst) { */ public static void configureLog4j() { LoggingTool localLogger = new LoggingTool(LoggingTool.class); - try { // NOPMD - org.apache.log4j.PropertyConfigurator.configure(LoggingTool.class - .getResource("/org/openscience/cdk/config/data/log4j.properties")); + try { + try (InputStream resourceAsStream = LoggingTool.class.getResourceAsStream("cdk-log4j2.xml")) { + if (resourceAsStream != null) { + XmlConfiguration config = new XmlConfiguration( + LoggerContext.getContext(true), + new ConfigurationSource(resourceAsStream)); + Configurator.reconfigure(config); + } + } } catch (NullPointerException e) { // NOPMD localLogger.error("Properties file not found: ", e.getMessage()); localLogger.debug(e); } catch (Exception e) { + e.printStackTrace(); localLogger.error("Unknown error occurred: ", e.getMessage()); localLogger.debug(e); } @@ -467,22 +497,22 @@ public static ILoggingTool create(Class sourceClass) { public void setLevel(int level) { switch (level) { case TRACE: - log4jLogger.setLevel(Level.TRACE); + Configurator.setLevel(log4jLogger.getName(), Level.TRACE); break; case DEBUG: - log4jLogger.setLevel(Level.DEBUG); + Configurator.setLevel(log4jLogger.getName(), Level.DEBUG); break; case INFO: - log4jLogger.setLevel(Level.INFO); + Configurator.setLevel(log4jLogger.getName(), Level.INFO); break; case WARN: - log4jLogger.setLevel(Level.WARN); + Configurator.setLevel(log4jLogger.getName(), Level.WARN); break; case ERROR: - log4jLogger.setLevel(Level.ERROR); + Configurator.setLevel(log4jLogger.getName(), Level.ERROR); break; case FATAL: - log4jLogger.setLevel(Level.FATAL); + Configurator.setLevel(log4jLogger.getName(), Level.FATAL); break; default: throw new IllegalArgumentException("Invalid log level: " + level); @@ -496,22 +526,10 @@ public void setLevel(int level) { public int getLevel() { Level level = log4jLogger.getLevel(); if (level == null) - level = Logger.getRootLogger().getLevel(); - switch (level.toInt()) { - case Level.TRACE_INT: - return TRACE; - case Level.DEBUG_INT: - return DEBUG; - case Level.INFO_INT: - return INFO; - case Level.WARN_INT: - return WARN; - case Level.ERROR_INT: - return ERROR; - case Level.FATAL_INT: - return FATAL; - default: - throw new IllegalArgumentException("Unsupported log4j level: " + level); - } + level = LogManager.getRootLogger().getLevel(); + Integer res = LOG4J2_LEVEL_TO_CDK_LEVEL.get(level); + if (res == null) + throw new IllegalArgumentException("Unsupported log4j level: " + level); + return res; } } diff --git a/misc/log4j/src/main/resources/org/openscience/cdk/config/data/log4j.properties b/misc/log4j/src/main/resources/org/openscience/cdk/config/data/log4j.properties deleted file mode 100644 index 0c42009c31d..00000000000 --- a/misc/log4j/src/main/resources/org/openscience/cdk/config/data/log4j.properties +++ /dev/null @@ -1,10 +0,0 @@ -log4j.rootCategory=DEBUG, file - -log4j.category.org.openscience.cdk=DEBUG - -log4j.appender.file=org.apache.log4j.FileAppender -log4j.appender.file.File=cdk.log -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n - -log4j.category.joelib=WARN diff --git a/misc/log4j/src/main/resources/org/openscience/cdk/tools/cdk-log4j2.xml b/misc/log4j/src/main/resources/org/openscience/cdk/tools/cdk-log4j2.xml new file mode 100644 index 00000000000..6d0278fcca5 --- /dev/null +++ b/misc/log4j/src/main/resources/org/openscience/cdk/tools/cdk-log4j2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/misc/log4j/src/test/java/org/openscience/cdk/tools/LoggingToolTest.java b/misc/log4j/src/test/java/org/openscience/cdk/tools/LoggingToolTest.java index 6f59f201537..ef7b5778019 100644 --- a/misc/log4j/src/test/java/org/openscience/cdk/tools/LoggingToolTest.java +++ b/misc/log4j/src/test/java/org/openscience/cdk/tools/LoggingToolTest.java @@ -19,7 +19,7 @@ */ package org.openscience.cdk.tools; -import org.apache.log4j.BasicConfigurator; +import org.apache.logging.log4j.core.config.Configurator; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -31,7 +31,7 @@ public class LoggingToolTest extends AbstractLoggingToolTest { @BeforeClass public static void ensureLog4JConfigured() { - BasicConfigurator.configure(); + Configurator.reconfigure(); } @Override