Skip to content

Commit 0a03ab7

Browse files
committed
improve tests, add support for INHERITED level string
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent eaa969f commit 0a03ab7

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/joran/PropertiesConfigurator.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import ch.qos.logback.classic.Logger;
1919
import ch.qos.logback.classic.LoggerContext;
2020
import ch.qos.logback.core.Context;
21+
import ch.qos.logback.core.joran.JoranConstants;
2122
import ch.qos.logback.core.joran.spi.JoranException;
2223
import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
2324
import ch.qos.logback.core.spi.ContextAwareBase;
25+
import ch.qos.logback.core.spi.ErrorCodes;
2426

2527
import java.io.File;
2628
import java.io.FileInputStream;
@@ -31,8 +33,9 @@
3133
import java.util.*;
3234

3335
import static ch.qos.logback.core.CoreConstants.DOT;
36+
import static ch.qos.logback.core.joran.JoranConstants.NULL;
3437

35-
public class PropertyConfigurator extends ContextAwareBase {
38+
public class PropertiesConfigurator extends ContextAwareBase {
3639

3740
static Comparator<String> LENGTH_COMPARATOR = new Comparator<String>() {
3841
@Override
@@ -78,10 +81,10 @@ public void doConfigure(URL url) throws JoranException {
7881
}
7982

8083
public void doConfigure(File file) throws JoranException {
81-
try(FileInputStream fileInputStream = new FileInputStream(file)) {
84+
try (FileInputStream fileInputStream = new FileInputStream(file)) {
8285
doConfigure(fileInputStream);
8386
} catch (IOException e) {
84-
throw new JoranException("Failed to load file "+file, e);
87+
throw new JoranException("Failed to load file " + file, e);
8588
}
8689
}
8790

@@ -103,7 +106,7 @@ public void doConfigure(InputStream inputStream) throws JoranException {
103106
}
104107

105108
private void close(InputStream inputStream) throws JoranException {
106-
if(inputStream != null) {
109+
if (inputStream != null) {
107110
try {
108111
inputStream.close();
109112
} catch (IOException e) {
@@ -139,10 +142,20 @@ void configureLoggers(Map<String, String> instructionMap) {
139142
}
140143
}
141144

142-
private void setLevel(String loggerName, String val) {
145+
private void setLevel(String loggerName, String levelStr) {
143146
Logger logger = getLoggerContext().getLogger(loggerName);
144-
Level level = Level.toLevel(val);
145-
logger.setLevel(level);
147+
148+
if (JoranConstants.INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
149+
if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(loggerName)) {
150+
addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL);
151+
} else {
152+
addInfo("Setting level of logger [" + loggerName + "] to null, i.e. INHERITED");
153+
logger.setLevel(null);
154+
}
155+
} else {
156+
Level level = Level.toLevel(levelStr);
157+
logger.setLevel(level);
158+
}
146159
}
147160

148161
private Map<String, String> extractVariablesMap(Properties properties) {

logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package ch.qos.logback.classic.joran;
1515

1616
import java.io.File;
17-
import java.io.FileInputStream;
1817
import java.net.URL;
1918
import java.util.List;
2019
import java.util.concurrent.ScheduledFuture;
@@ -90,10 +89,10 @@ public void run() {
9089

9190
private void runPropertiesConfigurator(File changedFile) {
9291
addInfo("Will run PropertyConfigurator on "+changedFile.getAbsolutePath());
93-
PropertyConfigurator propertyConfigurator = new PropertyConfigurator();
94-
propertyConfigurator.setContext(context);
92+
PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
93+
propertiesConfigurator.setContext(context);
9594
try {
96-
propertyConfigurator.doConfigure(changedFile);
95+
propertiesConfigurator.doConfigure(changedFile);
9796
context.fireConfigurationEvent(newPartialConfigurationEndedSuccessfullyEvent(this));
9897
} catch (JoranException e) {
9998
addError("Failed to reload "+ changedFile);

logback-classic/src/main/java/ch/qos/logback/classic/model/processor/PropertiesConfiguratorModelHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package ch.qos.logback.classic.model.processor;
1616

17-
import ch.qos.logback.classic.joran.PropertyConfigurator;
17+
import ch.qos.logback.classic.joran.PropertiesConfigurator;
1818
import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
1919
import ch.qos.logback.core.Context;
2020
import ch.qos.logback.core.joran.spi.JoranException;
@@ -61,10 +61,10 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
6161

6262
addInfo("Reading configuration from ["+getAttribureInUse()+"]");
6363

64-
PropertyConfigurator propertyConfigurator = new PropertyConfigurator();
65-
propertyConfigurator.setContext(mic.getContext());
64+
PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
65+
propertiesConfigurator.setContext(mic.getContext());
6666
try {
67-
propertyConfigurator.doConfigure(in);
67+
propertiesConfigurator.doConfigure(in);
6868
} catch (JoranException e) {
6969
addError("Could not configure from "+getAttribureInUse());
7070
throw new ModelHandlerException(e);

logback-classic/src/test/java/ch/qos/logback/classic/joran/PropertiesConfiguratorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
import static org.junit.jupiter.api.Assertions.assertEquals;
2727

28-
class PropertyConfiguratorTest {
28+
class PropertiesConfiguratorTest {
2929

3030
LoggerContext lc = new LoggerContext();
3131
Properties props = new Properties();
32-
PropertyConfigurator pc = new PropertyConfigurator();
32+
PropertiesConfigurator pc = new PropertiesConfigurator();
3333
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
3434
@BeforeEach
3535
public void setup() throws Exception {
@@ -39,8 +39,8 @@ public void setup() throws Exception {
3939
@Test
4040
public void smoke() {
4141
String TOTO_STR = "toto";
42-
props.setProperty(PropertyConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, Level.INFO.levelStr);
43-
props.setProperty(PropertyConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, Level.ERROR.levelStr);
42+
props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, Level.INFO.levelStr);
43+
props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, Level.ERROR.levelStr);
4444
pc.doConfigure(props);
4545

4646
Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
@@ -60,8 +60,8 @@ public void withVariables() {
6060

6161
props.setProperty(ROOT_LEVEL_STR, Level.INFO.levelStr);
6262
System.setProperty("totoLevel", Level.ERROR.levelStr);
63-
props.setProperty(PropertyConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, asVar(ROOT_LEVEL_STR));
64-
props.setProperty(PropertyConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, asVar(TOTO_LEVEL_STR));
63+
props.setProperty(PropertiesConfigurator.LOGBACK_ROOT_LOGGER_PREFIX, asVar(ROOT_LEVEL_STR));
64+
props.setProperty(PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + TOTO_STR, asVar(TOTO_LEVEL_STR));
6565
pc.doConfigure(props);
6666

6767
Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);

logback-classic/src/test/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTaskTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,23 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In
166166
String propertiesFileStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "roct-" + diff + ".properties";
167167
File propertiesFile = new File(propertiesFileStr);
168168
String configurationStr = "<configuration scan=\"true\" scanPeriod=\"1 millisecond\"><propertiesConfigurator file=\"" + propertiesFileStr + "\"/></configuration>";
169-
writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=INFO");
169+
writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=INFO");
170170
configure(asBAIS(configurationStr));
171171
Logger abcLogger = loggerContext.getLogger(loggerName);
172172
assertEquals(Level.INFO, abcLogger.getLevel());
173173

174174
CountDownLatch changeDetectedLatch0 = registerChangeDetectedListener();
175175
CountDownLatch configurationDoneLatch0 = registerPartialConfigurationEndedSuccessfullyEventListener();
176176

177-
writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=WARN");
177+
writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=WARN");
178178
changeDetectedLatch0.await();
179179
configurationDoneLatch0.await();
180180
assertEquals(Level.WARN, abcLogger.getLevel());
181181

182182

183183
CountDownLatch changeDetectedLatch1 = registerChangeDetectedListener();
184184
CountDownLatch configurationDoneLatch1 = registerPartialConfigurationEndedSuccessfullyEventListener();
185-
writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=ERROR");
185+
writeToFile(propertiesFile, PropertiesConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=ERROR");
186186
changeDetectedLatch1.await();
187187
configurationDoneLatch1.await();
188188
assertEquals(Level.ERROR, abcLogger.getLevel());

0 commit comments

Comments
 (0)