Skip to content

Commit

Permalink
Fixed loading configuration from system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwmedia committed Sep 29, 2015
1 parent 2b35a2e commit ed52182
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
28 changes: 21 additions & 7 deletions tests/src/org/pmw/tinylog/RegressionsTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2012 Martin Winandy
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
Expand Down Expand Up @@ -61,7 +61,7 @@ public final void testWrongClass() {

/**
* Bug: If a log file is continued, the policy will start from scratch. This leads to a too late rollover.
*
*
* @throws Exception
* Test failed
*/
Expand Down Expand Up @@ -127,7 +127,7 @@ public final void testLowerCustomLoggingLevelsForPackages() {

/**
* Bug: Timestamps need a locale but the locale isn't set at startup.
*
*
* @throws Exception
* Test failed
*/
Expand All @@ -139,7 +139,7 @@ public final void testTimestampLabelerAtStartup() throws Exception {

/**
* Bug: Rolling fails for files without a parent path in timestamp labeler.
*
*
* @throws IOException
* Test failed
*/
Expand All @@ -160,7 +160,7 @@ public final void testTimestampLabelerRolling() throws IOException {
/**
* Bug: Initialization of a rolling file writer with a timestamp labeler fails if there is no previous
* configuration.
*
*
* @throws Exception
* Test failed
*/
Expand Down Expand Up @@ -192,6 +192,20 @@ public final void testLogExceptionWithEmptyStackTrace() {
assertEquals(exception.getClass().getName() + EnvironmentHelper.getNewLine(), logEntry.getRenderedLogEntry());
}

/**
* Bug: System properties not loaded without existing properties file in default package.
*/
@Test
public final void testSystemProperties() {
try {
System.setProperty("tinylog.level", "trace");
Configurator.init().activate();
assertEquals(Level.TRACE, Logger.getLevel());
} finally {
System.clearProperty("tinylog.level");
}
}

private void resetLogger() throws Exception {
Field field = Logger.class.getDeclaredField("configuration");
field.setAccessible(true);
Expand Down
18 changes: 9 additions & 9 deletions tinylog/src/org/pmw/tinylog/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,17 @@ static Configurator init() {
}
}

if (stream == null) {
return Configurator.defaultConfig();
} else {
Properties systemProperties = System.getProperties();
for (Object key : systemProperties.keySet()) {
String name = (String) key;
if (name.startsWith("tinylog.")) {
properties.put(name, systemProperties.getProperty(name));
}
Properties systemProperties = System.getProperties();
for (Object key : systemProperties.keySet()) {
String name = (String) key;
if (name.startsWith("tinylog.")) {
properties.put(name, systemProperties.getProperty(name));
}
}

if (properties.isEmpty()) {
return Configurator.defaultConfig();
} else {
if ("true".equalsIgnoreCase(properties.getProperty("tinylog.configuration.observe"))) {
shutdownWritingThread(true);
Configurator configurator = PropertiesLoader.readProperties(properties);
Expand Down

0 comments on commit ed52182

Please sign in to comment.