Re-initialize logging if Environment changed settings
Fixes gh-273
This commit is contained in:
parent
5c6690f704
commit
a26ae0a303
|
|
@ -153,16 +153,22 @@ public class LoggingApplicationListener implements SmartApplicationListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean environmentChanged = false;
|
||||||
for (Map.Entry<String, String> mapping : ENVIRONMENT_SYSTEM_PROPERTY_MAPPING
|
for (Map.Entry<String, String> mapping : ENVIRONMENT_SYSTEM_PROPERTY_MAPPING
|
||||||
.entrySet()) {
|
.entrySet()) {
|
||||||
if (environment.containsProperty(mapping.getKey())) {
|
if (environment.containsProperty(mapping.getKey())) {
|
||||||
System.setProperty(mapping.getValue(),
|
System.setProperty(mapping.getValue(),
|
||||||
environment.getProperty(mapping.getKey()));
|
environment.getProperty(mapping.getKey()));
|
||||||
|
environmentChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LoggingSystem system = LoggingSystem.get(classLoader);
|
LoggingSystem system = LoggingSystem.get(classLoader);
|
||||||
|
|
||||||
|
if (environmentChanged) {
|
||||||
|
// Re-initialize the defaults in case the Environment changed
|
||||||
|
system.beforeInitialize();
|
||||||
|
}
|
||||||
// User specified configuration
|
// User specified configuration
|
||||||
if (environment.containsProperty("logging.config")) {
|
if (environment.containsProperty("logging.config")) {
|
||||||
String value = environment.getProperty("logging.config");
|
String value = environment.getProperty("logging.config");
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.context.listener;
|
package org.springframework.boot.context.listener;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.logging.LogManager;
|
import java.util.logging.LogManager;
|
||||||
|
|
||||||
|
|
@ -71,6 +72,7 @@ public class LoggingApplicationListenerTests {
|
||||||
JavaLoggingSystem.class.getResourceAsStream("logging.properties"));
|
JavaLoggingSystem.class.getResourceAsStream("logging.properties"));
|
||||||
this.initializer.onApplicationEvent(new SpringApplicationStartEvent(
|
this.initializer.onApplicationEvent(new SpringApplicationStartEvent(
|
||||||
new SpringApplication(), NO_ARGS));
|
new SpringApplication(), NO_ARGS));
|
||||||
|
new File("target/foo.log").delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
@ -119,25 +121,37 @@ public class LoggingApplicationListenerTests {
|
||||||
public void testAddLogFileProperty() {
|
public void testAddLogFileProperty() {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
EnvironmentTestUtils.addEnvironment(this.context,
|
||||||
"logging.config: classpath:logback-nondefault.xml",
|
"logging.config: classpath:logback-nondefault.xml",
|
||||||
"logging.file: foo.log");
|
"logging.file: target/foo.log");
|
||||||
this.initializer.initialize(this.context.getEnvironment(),
|
this.initializer.initialize(this.context.getEnvironment(),
|
||||||
this.context.getClassLoader());
|
this.context.getClassLoader());
|
||||||
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
|
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
|
||||||
logger.info("Hello world");
|
logger.info("Hello world");
|
||||||
String output = this.outputCapture.toString().trim();
|
String output = this.outputCapture.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.startsWith("foo.log"));
|
assertTrue("Wrong output:\n" + output, output.startsWith("target/foo.log"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddLogFilePropertyWithDefault() {
|
||||||
|
assertFalse(new File("target/foo.log").exists());
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context, "logging.file: target/foo.log");
|
||||||
|
this.initializer.initialize(this.context.getEnvironment(),
|
||||||
|
this.context.getClassLoader());
|
||||||
|
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
|
||||||
|
logger.info("Hello world");
|
||||||
|
assertTrue(new File("target/foo.log").exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddLogPathProperty() {
|
public void testAddLogPathProperty() {
|
||||||
EnvironmentTestUtils.addEnvironment(this.context,
|
EnvironmentTestUtils.addEnvironment(this.context,
|
||||||
"logging.config: classpath:logback-nondefault.xml", "logging.path: foo/");
|
"logging.config: classpath:logback-nondefault.xml",
|
||||||
|
"logging.path: target/foo/");
|
||||||
this.initializer.initialize(this.context.getEnvironment(),
|
this.initializer.initialize(this.context.getEnvironment(),
|
||||||
this.context.getClassLoader());
|
this.context.getClassLoader());
|
||||||
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
|
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
|
||||||
logger.info("Hello world");
|
logger.info("Hello world");
|
||||||
String output = this.outputCapture.toString().trim();
|
String output = this.outputCapture.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.startsWith("foo/spring.log"));
|
assertTrue("Wrong output:\n" + output, output.startsWith("target/foo/spring.log"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue