Skip to content

Commit

Permalink
improve tests, add support for INHERITED level string
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Sep 7, 2024
1 parent eaa969f commit 0a03ab7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> LENGTH_COMPARATOR = new Comparator<String>() {
@Override
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -139,10 +142,20 @@ void configureLoggers(Map<String, String> 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<String, String> extractVariablesMap(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,23 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In
String propertiesFileStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "roct-" + diff + ".properties";
File propertiesFile = new File(propertiesFileStr);
String configurationStr = "<configuration scan=\"true\" scanPeriod=\"1 millisecond\"><propertiesConfigurator file=\"" + propertiesFileStr + "\"/></configuration>";
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());

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());


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());
Expand Down

0 comments on commit 0a03ab7

Please sign in to comment.