From c0536d5cb99445d5e4aa59b50c8630aeaaec1a32 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 24 Jun 2022 20:18:07 +0200 Subject: [PATCH] Downgrade slf4j to 1.7.36 --- .../pmd/ant/internal/PMDTaskImpl.java | 2 +- .../internal/Slf4jSimpleConfiguration.java | 38 +++++++++++++------ .../pmd/lang/ast/SemanticErrorReporter.java | 20 +++++++++- .../log/internal/SimpleMessageReporter.java | 35 ++++++++++++++++- .../org/slf4j/PmdLoggerFactoryFriend.java | 26 +++++++++++++ .../net/sourceforge/pmd/cli/CoreCliTest.java | 9 +++-- pom.xml | 2 +- 7 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 pmd-core/src/main/java/org/slf4j/PmdLoggerFactoryFriend.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java index 4117010f49..290032ed92 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/ant/internal/PMDTaskImpl.java @@ -239,7 +239,7 @@ public class PMDTaskImpl { Slf4jSimpleConfiguration.installJulBridge(); // need to reload the logger with the new configuration Logger log = LoggerFactory.getLogger(PMDTaskImpl.class); - log.atLevel(level).log("Logging is at {}", level); + log.info("Logging is at {}", level); try { doTask(); } finally { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/internal/Slf4jSimpleConfiguration.java b/pmd-core/src/main/java/net/sourceforge/pmd/internal/Slf4jSimpleConfiguration.java index eaa1b37581..44254440ad 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/internal/Slf4jSimpleConfiguration.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/internal/Slf4jSimpleConfiguration.java @@ -5,20 +5,17 @@ package net.sourceforge.pmd.internal; import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; import org.slf4j.ILoggerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.slf4j.LoggerFactoryFriend; +import org.slf4j.PmdLoggerFactoryFriend; import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.event.Level; public final class Slf4jSimpleConfiguration { - private static final String SIMPLE_LOGGER_FACTORY_CLASS = "org.slf4j.simple.SimpleLoggerFactory"; - private static final String SIMPLE_LOGGER_CLASS = "org.slf4j.simple.SimpleLogger"; + private static final String SIMPLE_LOGGER_FACTORY_CLASS = "org.slf4j.impl.SimpleLoggerFactory"; + private static final String SIMPLE_LOGGER_CLASS = "org.slf4j.impl.SimpleLogger"; private Slf4jSimpleConfiguration() { } @@ -43,22 +40,39 @@ public final class Slf4jSimpleConfiguration { Method initMethod = simpleLoggerClass.getDeclaredMethod("init"); initMethod.setAccessible(true); initMethod.invoke(null); + + // Call SimpleLoggerFactory.reset() by reflection. + Method resetMethod = loggerFactoryClass.getDeclaredMethod("reset"); + resetMethod.setAccessible(true); + resetMethod.invoke(loggerFactory); } catch (ReflectiveOperationException ex) { System.err.println("Error while initializing logging: " + ex); } - LoggerFactoryFriend.reset(); + PmdLoggerFactoryFriend.reset(); } public static Level getDefaultLogLevel() { Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - List enabledLogLevels = Arrays.asList(Level.values()).stream() - .filter(rootLogger::isEnabledForLevel).collect(Collectors.toList()); - if (enabledLogLevels.isEmpty()) { + + // check the lowest log level first + if (rootLogger.isTraceEnabled()) { + return Level.TRACE; + } + if (rootLogger.isDebugEnabled()) { + return Level.DEBUG; + } + if (rootLogger.isInfoEnabled()) { return Level.INFO; } - // return the lowest (last) level - return enabledLogLevels.get(enabledLogLevels.size() - 1); + if (rootLogger.isWarnEnabled()) { + return Level.WARN; + } + if (rootLogger.isErrorEnabled()) { + return Level.ERROR; + } + + return Level.INFO; } public static void disableLogging(Class clazz) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java index df76bfe1fc..2be24ceaf0 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java @@ -83,7 +83,25 @@ public interface SemanticErrorReporter { private String logMessage(Level level, Node location, String message, Object[] args) { String fullMessage = makeMessage(location, message, args); - logger.atLevel(level).log(fullMessage); + switch (level) { + case ERROR: + logger.error(fullMessage); + break; + case WARN: + logger.warn(fullMessage); + break; + case INFO: + logger.info(fullMessage); + break; + case DEBUG: + logger.debug(fullMessage); + break; + case TRACE: + logger.trace(fullMessage); + break; + default: + throw new AssertionError("Invalid log level: " + level); + } return fullMessage; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/internal/SimpleMessageReporter.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/internal/SimpleMessageReporter.java index 672e40df98..6481f8b8c9 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/internal/SimpleMessageReporter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/internal/SimpleMessageReporter.java @@ -26,11 +26,42 @@ public class SimpleMessageReporter extends MessageReporterBase implements Messag @Override protected boolean isLoggableImpl(Level level) { - return backend.isEnabledForLevel(level); + switch (level) { + case ERROR: + return backend.isErrorEnabled(); + case WARN: + return backend.isWarnEnabled(); + case INFO: + return backend.isInfoEnabled(); + case DEBUG: + return backend.isDebugEnabled(); + case TRACE: + return backend.isTraceEnabled(); + default: + return false; + } } @Override protected void logImpl(Level level, String message, Object[] formatArgs) { - backend.atLevel(level).log(message, formatArgs); + switch (level) { + case ERROR: + backend.error(message, formatArgs); + break; + case WARN: + backend.warn(message, formatArgs); + break; + case INFO: + backend.info(message, formatArgs); + break; + case DEBUG: + backend.debug(message, formatArgs); + break; + case TRACE: + backend.trace(message, formatArgs); + break; + default: + throw new AssertionError("Invalid log level: " + level); + } } } diff --git a/pmd-core/src/main/java/org/slf4j/PmdLoggerFactoryFriend.java b/pmd-core/src/main/java/org/slf4j/PmdLoggerFactoryFriend.java new file mode 100644 index 0000000000..9ea8426926 --- /dev/null +++ b/pmd-core/src/main/java/org/slf4j/PmdLoggerFactoryFriend.java @@ -0,0 +1,26 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package org.slf4j; + +import net.sourceforge.pmd.annotation.InternalApi; + +/** + * This class is for internal use only. + *

+ * It is needed to reinitialize the underlying logging in case the configuration is changed. + *

+ * @deprecated internal, do not use + */ +@Deprecated +@InternalApi +public final class PmdLoggerFactoryFriend { + private PmdLoggerFactoryFriend() { + // helper + } + + public static void reset() { + LoggerFactory.reset(); + } +} diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/cli/CoreCliTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/cli/CoreCliTest.java index 1b71d3f87a..a9dc3c2b59 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/cli/CoreCliTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/cli/CoreCliTest.java @@ -195,19 +195,22 @@ public class CoreCliTest { @Test public void debugLogging() { - runPmdSuccessfully("--debug", "--no-cache", "--dir", srcDir, "--rulesets", DUMMY_RULESET); + Path reportFile = tempRoot().resolve("out/reportFile.txt"); + runPmdSuccessfully("--debug", "--no-cache", "--dir", srcDir, "--rulesets", DUMMY_RULESET, "-r", reportFile); assertThat(errStreamCaptor.getLog(), containsString("[main] INFO net.sourceforge.pmd.PMD - Log level is at TRACE")); } @Test public void defaultLogging() { - runPmdSuccessfully("--no-cache", "--dir", srcDir, "--rulesets", DUMMY_RULESET); + Path reportFile = tempRoot().resolve("out/reportFile.txt"); + runPmdSuccessfully("--no-cache", "--dir", srcDir, "--rulesets", DUMMY_RULESET, "-r", reportFile); assertThat(errStreamCaptor.getLog(), containsString("[main] INFO net.sourceforge.pmd.PMD - Log level is at INFO")); } @Test public void testDeprecatedRulesetSyntaxOnCommandLine() { - runPmd(StatusCode.VIOLATIONS_FOUND, "--no-cache", "--dir", srcDir, "--rulesets", "dummy-basic"); + Path reportFile = tempRoot().resolve("out/reportFile.txt"); + runPmd(StatusCode.VIOLATIONS_FOUND, "--no-cache", "--dir", srcDir, "--rulesets", "dummy-basic", "-r", reportFile); MatcherAssert.assertThat(errStreamCaptor.getLog(), containsString("Ruleset reference 'dummy-basic' uses a deprecated form, use 'rulesets/dummy/basic.xml' instead")); } diff --git a/pom.xml b/pom.xml index 2d76adc8d3..6a6c466086 100644 --- a/pom.xml +++ b/pom.xml @@ -98,7 +98,7 @@ 1.10.12 3.2.0 4.8 - 2.0.0-alpha6 + 1.7.36 10.7 UTF-8