Downgrade slf4j to 1.7.36

This commit is contained in:
Andreas Dangel
2022-06-24 20:18:07 +02:00
parent 0f58afcc01
commit c0536d5cb9
7 changed files with 112 additions and 20 deletions

View File

@ -239,7 +239,7 @@ public class PMDTaskImpl {
Slf4jSimpleConfiguration.installJulBridge(); Slf4jSimpleConfiguration.installJulBridge();
// need to reload the logger with the new configuration // need to reload the logger with the new configuration
Logger log = LoggerFactory.getLogger(PMDTaskImpl.class); Logger log = LoggerFactory.getLogger(PMDTaskImpl.class);
log.atLevel(level).log("Logging is at {}", level); log.info("Logging is at {}", level);
try { try {
doTask(); doTask();
} finally { } finally {

View File

@ -5,20 +5,17 @@
package net.sourceforge.pmd.internal; package net.sourceforge.pmd.internal;
import java.lang.reflect.Method; 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.ILoggerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactoryFriend; import org.slf4j.PmdLoggerFactoryFriend;
import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.bridge.SLF4JBridgeHandler;
import org.slf4j.event.Level; import org.slf4j.event.Level;
public final class Slf4jSimpleConfiguration { public final class Slf4jSimpleConfiguration {
private static final String SIMPLE_LOGGER_FACTORY_CLASS = "org.slf4j.simple.SimpleLoggerFactory"; private static final String SIMPLE_LOGGER_FACTORY_CLASS = "org.slf4j.impl.SimpleLoggerFactory";
private static final String SIMPLE_LOGGER_CLASS = "org.slf4j.simple.SimpleLogger"; private static final String SIMPLE_LOGGER_CLASS = "org.slf4j.impl.SimpleLogger";
private Slf4jSimpleConfiguration() { } private Slf4jSimpleConfiguration() { }
@ -43,22 +40,39 @@ public final class Slf4jSimpleConfiguration {
Method initMethod = simpleLoggerClass.getDeclaredMethod("init"); Method initMethod = simpleLoggerClass.getDeclaredMethod("init");
initMethod.setAccessible(true); initMethod.setAccessible(true);
initMethod.invoke(null); initMethod.invoke(null);
// Call SimpleLoggerFactory.reset() by reflection.
Method resetMethod = loggerFactoryClass.getDeclaredMethod("reset");
resetMethod.setAccessible(true);
resetMethod.invoke(loggerFactory);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
System.err.println("Error while initializing logging: " + ex); System.err.println("Error while initializing logging: " + ex);
} }
LoggerFactoryFriend.reset(); PmdLoggerFactoryFriend.reset();
} }
public static Level getDefaultLogLevel() { public static Level getDefaultLogLevel() {
Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
List<Level> enabledLogLevels = Arrays.asList(Level.values()).stream()
.filter(rootLogger::isEnabledForLevel).collect(Collectors.toList()); // check the lowest log level first
if (enabledLogLevels.isEmpty()) { if (rootLogger.isTraceEnabled()) {
return Level.TRACE;
}
if (rootLogger.isDebugEnabled()) {
return Level.DEBUG;
}
if (rootLogger.isInfoEnabled()) {
return Level.INFO; return Level.INFO;
} }
// return the lowest (last) level if (rootLogger.isWarnEnabled()) {
return enabledLogLevels.get(enabledLogLevels.size() - 1); return Level.WARN;
}
if (rootLogger.isErrorEnabled()) {
return Level.ERROR;
}
return Level.INFO;
} }
public static void disableLogging(Class<?> clazz) { public static void disableLogging(Class<?> clazz) {

View File

@ -83,7 +83,25 @@ public interface SemanticErrorReporter {
private String logMessage(Level level, Node location, String message, Object[] args) { private String logMessage(Level level, Node location, String message, Object[] args) {
String fullMessage = makeMessage(location, message, 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; return fullMessage;
} }

View File

@ -26,11 +26,42 @@ public class SimpleMessageReporter extends MessageReporterBase implements Messag
@Override @Override
protected boolean isLoggableImpl(Level level) { 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 @Override
protected void logImpl(Level level, String message, Object[] formatArgs) { 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);
}
} }
} }

View File

@ -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.
* <p>
* It is needed to reinitialize the underlying logging in case the configuration is changed.
* </p>
* @deprecated internal, do not use
*/
@Deprecated
@InternalApi
public final class PmdLoggerFactoryFriend {
private PmdLoggerFactoryFriend() {
// helper
}
public static void reset() {
LoggerFactory.reset();
}
}

View File

@ -195,19 +195,22 @@ public class CoreCliTest {
@Test @Test
public void debugLogging() { 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")); assertThat(errStreamCaptor.getLog(), containsString("[main] INFO net.sourceforge.pmd.PMD - Log level is at TRACE"));
} }
@Test @Test
public void defaultLogging() { 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")); assertThat(errStreamCaptor.getLog(), containsString("[main] INFO net.sourceforge.pmd.PMD - Log level is at INFO"));
} }
@Test @Test
public void testDeprecatedRulesetSyntaxOnCommandLine() { 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")); MatcherAssert.assertThat(errStreamCaptor.getLog(), containsString("Ruleset reference 'dummy-basic' uses a deprecated form, use 'rulesets/dummy/basic.xml' instead"));
} }

View File

@ -98,7 +98,7 @@
<ant.version>1.10.12</ant.version> <ant.version>1.10.12</ant.version>
<javadoc.plugin.version>3.2.0</javadoc.plugin.version> <javadoc.plugin.version>3.2.0</javadoc.plugin.version>
<antlr.version>4.8</antlr.version> <antlr.version>4.8</antlr.version>
<slf4j.version>2.0.0-alpha6</slf4j.version> <slf4j.version>1.7.36</slf4j.version>
<saxon.version>10.7</saxon.version> <saxon.version>10.7</saxon.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>