Skip to content

Commit 4af17fb

Browse files
maarztctrueden
authored andcommitted
Allow feeding LogService config via Properties
And test that Properties-based configuration works. Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
1 parent 2407a38 commit 4af17fb

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/main/java/org/scijava/log/AbstractLogService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ public abstract class AbstractLogService extends AbstractService implements
7171
// -- constructor --
7272

7373
public AbstractLogService() {
74+
this(System.getProperties());
75+
}
76+
77+
public AbstractLogService(final Properties properties) {
7478
// check SciJava log level system properties for initial logging levels
7579

7680
// global log level property
77-
final String logProp = System.getProperty(LOG_LEVEL_PROPERTY);
81+
final String logProp = properties.getProperty(LOG_LEVEL_PROPERTY);
7882
final int level = LogLevel.value(logProp);
7983
if (level >= 0) setLevel(level);
8084

@@ -83,14 +87,13 @@ public AbstractLogService() {
8387

8488
// populate custom class- and package-specific log level properties
8589
final String logLevelPrefix = LOG_LEVEL_PROPERTY + ":";
86-
final Properties props = System.getProperties();
87-
for (final Object propKey : props.keySet()) {
90+
for (final Object propKey : properties.keySet()) {
8891
if (!(propKey instanceof String)) continue;
8992
final String propName = (String) propKey;
9093
if (!propName.startsWith(logLevelPrefix)) continue;
9194
final String classOrPackageName =
9295
propName.substring(logLevelPrefix.length());
93-
setLevel(classOrPackageName, LogLevel.value(props.getProperty(propName)));
96+
setLevel(classOrPackageName, LogLevel.value(properties.getProperty(propName)));
9497
}
9598

9699
}

src/test/java/org/scijava/log/LogServiceTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import java.util.Properties;
56
import java.util.function.BiConsumer;
67
import java.util.function.Function;
78

@@ -116,6 +117,54 @@ private void testIsLevel(int level, Function<LogService, Boolean> isLevel) {
116117
assertTrue(isLevel.apply(logService));
117118
}
118119

120+
@Test
121+
public void testDefaultLevel() {
122+
assertEquals(LogLevel.INFO, new TestableLogService().getLevel());
123+
}
124+
125+
@Test
126+
public void testMainSystemProperty() {
127+
Properties properties = new Properties();
128+
properties.setProperty(LogService.LOG_LEVEL_PROPERTY, "error");
129+
int level = new TestableLogService(properties).getLevel();
130+
assertEquals(LogLevel.ERROR, level);
131+
}
132+
133+
static class Dummy {
134+
public static int getLevel(LogService log) {
135+
return log.getLevel();
136+
}
137+
}
138+
139+
@Test
140+
public void testClassLogLevel() {
141+
final TestableLogService log = new TestableLogService();
142+
log.setLevel(LogLevel.DEBUG);
143+
log.setLevel(Dummy.class.getName(), LogLevel.ERROR);
144+
int level = Dummy.getLevel(log);
145+
assertEquals(LogLevel.ERROR, level);
146+
}
147+
148+
@Test
149+
public void testClassLogLevelViaProperties() {
150+
Properties properties = new Properties();
151+
properties.setProperty(LogService.LOG_LEVEL_PROPERTY + ":" + Dummy.class.getName(), LogLevel.prefix(LogLevel.ERROR));
152+
properties.setProperty(LogService.LOG_LEVEL_PROPERTY + ":" + this.getClass().getName(), LogLevel.prefix(LogLevel.TRACE));
153+
final LogService log = new TestableLogService(properties);
154+
log.setLevel(LogLevel.DEBUG);
155+
int level = Dummy.getLevel(log);
156+
assertEquals(LogLevel.ERROR, level);
157+
}
158+
159+
@Test
160+
public void testPackageLogLevel() {
161+
final LogService log = new TestableLogService();
162+
log.setLevel("org.scijava.log", LogLevel.TRACE);
163+
log.setLevel("xyz.foo.bar", LogLevel.ERROR);
164+
int level = log.getLevel();
165+
assertEquals(LogLevel.TRACE, level);
166+
}
167+
119168
// -- Helper classes --
120169

121170
private static class MyTestClass {
@@ -140,6 +189,14 @@ private static class TestableLogService extends AbstractLogService {
140189
String message = null;
141190
Throwable exception = null;
142191

192+
public TestableLogService() {
193+
this(new Properties());
194+
}
195+
196+
public TestableLogService(Properties properties) {
197+
super(properties);
198+
}
199+
143200
public String message() {
144201
return message;
145202
}

0 commit comments

Comments
 (0)