diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/joran/PropertiesConfigurator.java b/logback-classic/src/main/java/ch/qos/logback/classic/joran/PropertiesConfigurator.java index eec8f63290..f143122d3f 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/joran/PropertiesConfigurator.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/joran/PropertiesConfigurator.java @@ -18,9 +18,11 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.core.Context; +import ch.qos.logback.core.joran.JoranConstants; import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.model.util.VariableSubstitutionsHelper; import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.spi.ErrorCodes; import java.io.File; import java.io.FileInputStream; @@ -31,8 +33,9 @@ import java.util.*; import static ch.qos.logback.core.CoreConstants.DOT; +import static ch.qos.logback.core.joran.JoranConstants.NULL; -public class PropertyConfigurator extends ContextAwareBase { +public class PropertiesConfigurator extends ContextAwareBase { static Comparator LENGTH_COMPARATOR = new Comparator() { @Override @@ -78,10 +81,10 @@ public void doConfigure(URL url) throws JoranException { } public void doConfigure(File file) throws JoranException { - try(FileInputStream fileInputStream = new FileInputStream(file)) { + try (FileInputStream fileInputStream = new FileInputStream(file)) { doConfigure(fileInputStream); } catch (IOException e) { - throw new JoranException("Failed to load file "+file, e); + throw new JoranException("Failed to load file " + file, e); } } @@ -103,7 +106,7 @@ public void doConfigure(InputStream inputStream) throws JoranException { } private void close(InputStream inputStream) throws JoranException { - if(inputStream != null) { + if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { @@ -139,10 +142,20 @@ void configureLoggers(Map instructionMap) { } } - private void setLevel(String loggerName, String val) { + private void setLevel(String loggerName, String levelStr) { Logger logger = getLoggerContext().getLogger(loggerName); - Level level = Level.toLevel(val); - logger.setLevel(level); + + if (JoranConstants.INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) { + if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(loggerName)) { + addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL); + } else { + addInfo("Setting level of logger [" + loggerName + "] to null, i.e. INHERITED"); + logger.setLevel(null); + } + } else { + Level level = Level.toLevel(levelStr); + logger.setLevel(level); + } } private Map extractVariablesMap(Properties properties) { diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java b/logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java index 5202331cb9..d5945eb1ca 100755 --- a/logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java @@ -14,7 +14,6 @@ package ch.qos.logback.classic.joran; import java.io.File; -import java.io.FileInputStream; import java.net.URL; import java.util.List; import java.util.concurrent.ScheduledFuture; @@ -90,10 +89,10 @@ public void run() { private void runPropertiesConfigurator(File changedFile) { addInfo("Will run PropertyConfigurator on "+changedFile.getAbsolutePath()); - PropertyConfigurator propertyConfigurator = new PropertyConfigurator(); - propertyConfigurator.setContext(context); + PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator(); + propertiesConfigurator.setContext(context); try { - propertyConfigurator.doConfigure(changedFile); + propertiesConfigurator.doConfigure(changedFile); context.fireConfigurationEvent(newPartialConfigurationEndedSuccessfullyEvent(this)); } catch (JoranException e) { addError("Failed to reload "+ changedFile); diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java b/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java index 65c635d358..27cb776729 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java @@ -14,7 +14,7 @@ package ch.qos.logback.classic.model.processor; -import ch.qos.logback.classic.joran.PropertyConfigurator; +import ch.qos.logback.classic.joran.PropertiesConfigurator; import ch.qos.logback.classic.model.PropertiesConfiguratorModel; import ch.qos.logback.core.Context; import ch.qos.logback.core.joran.spi.JoranException; @@ -61,10 +61,10 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand addInfo("Reading configuration from ["+getAttribureInUse()+"]"); - PropertyConfigurator propertyConfigurator = new PropertyConfigurator(); - propertyConfigurator.setContext(mic.getContext()); + PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator(); + propertiesConfigurator.setContext(mic.getContext()); try { - propertyConfigurator.doConfigure(in); + propertiesConfigurator.doConfigure(in); } catch (JoranException e) { addError("Could not configure from "+getAttribureInUse()); throw new ModelHandlerException(e); diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/joran/PropertiesConfiguratorTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/joran/PropertiesConfiguratorTest.java index fa641d52a7..7c3139804a 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/joran/PropertiesConfiguratorTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/joran/PropertiesConfiguratorTest.java @@ -25,11 +25,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -class PropertyConfiguratorTest { +class PropertiesConfiguratorTest { LoggerContext lc = new LoggerContext(); Properties props = new Properties(); - PropertyConfigurator pc = new PropertyConfigurator(); + PropertiesConfigurator pc = new PropertiesConfigurator(); StatusPrinter2 statusPrinter2 = new StatusPrinter2(); @BeforeEach public void setup() throws Exception { @@ -39,8 +39,8 @@ public void setup() throws Exception { @Test public void smoke() { String TOTO_STR = "toto"; - props.setProperty(PropertyConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, Level.INFO.levelStr); - props.setProperty(PropertyConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, Level.ERROR.levelStr); + props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, Level.INFO.levelStr); + props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, Level.ERROR.levelStr); pc.doConfigure(props); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); @@ -60,8 +60,8 @@ public void withVariables() { props.setProperty(ROOT_LEVEL_STR, Level.INFO.levelStr); System.setProperty("totoLevel", Level.ERROR.levelStr); - props.setProperty(PropertyConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, asVar(ROOT_LEVEL_STR)); - props.setProperty(PropertyConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, asVar(TOTO_LEVEL_STR)); + props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, asVar(ROOT_LEVEL_STR)); + props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, asVar(TOTO_LEVEL_STR)); pc.doConfigure(props); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTaskTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTaskTest.java index 980518e44d..c946888a40 100755 --- a/logback-classic/src/test/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTaskTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTaskTest.java @@ -166,7 +166,7 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In String propertiesFileStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "roct-" + diff + ".properties"; File propertiesFile = new File(propertiesFileStr); String configurationStr = ""; - writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=INFO"); + writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=INFO"); configure(asBAIS(configurationStr)); Logger abcLogger = loggerContext.getLogger(loggerName); assertEquals(Level.INFO, abcLogger.getLevel()); @@ -174,7 +174,7 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In CountDownLatch changeDetectedLatch0 = registerChangeDetectedListener(); CountDownLatch configurationDoneLatch0 = registerPartialConfigurationEndedSuccessfullyEventListener(); - writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=WARN"); + writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=WARN"); changeDetectedLatch0.await(); configurationDoneLatch0.await(); assertEquals(Level.WARN, abcLogger.getLevel()); @@ -182,7 +182,7 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In CountDownLatch changeDetectedLatch1 = registerChangeDetectedListener(); CountDownLatch configurationDoneLatch1 = registerPartialConfigurationEndedSuccessfullyEventListener(); - writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=ERROR"); + writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=ERROR"); changeDetectedLatch1.await(); configurationDoneLatch1.await(); assertEquals(Level.ERROR, abcLogger.getLevel());