Commit 4eda91d6 authored by Andy Wilkinson's avatar Andy Wilkinson

Allow logging.file and logging.path to refer to logging system properties

Closes gh-5274
parent 7a4e061d
...@@ -247,16 +247,18 @@ public class LoggingApplicationListener implements GenericApplicationListener { ...@@ -247,16 +247,18 @@ public class LoggingApplicationListener implements GenericApplicationListener {
*/ */
protected void initialize(ConfigurableEnvironment environment, protected void initialize(ConfigurableEnvironment environment,
ClassLoader classLoader) { ClassLoader classLoader) {
setSystemProperties(environment);
LogFile logFile = LogFile.get(environment); LogFile logFile = LogFile.get(environment);
setSystemProperties(environment, logFile); if (logFile != null) {
logFile.applyToSystemProperties();
}
initializeEarlyLoggingLevel(environment); initializeEarlyLoggingLevel(environment);
initializeSystem(environment, this.loggingSystem, logFile); initializeSystem(environment, this.loggingSystem, logFile);
initializeFinalLoggingLevels(environment, this.loggingSystem); initializeFinalLoggingLevels(environment, this.loggingSystem);
registerShutdownHookIfNecessary(environment, this.loggingSystem); registerShutdownHookIfNecessary(environment, this.loggingSystem);
} }
private void setSystemProperties(ConfigurableEnvironment environment, private void setSystemProperties(ConfigurableEnvironment environment) {
LogFile logFile) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver( RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
environment, "logging."); environment, "logging.");
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD, setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
...@@ -265,9 +267,6 @@ public class LoggingApplicationListener implements GenericApplicationListener { ...@@ -265,9 +267,6 @@ public class LoggingApplicationListener implements GenericApplicationListener {
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file"); setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level"); setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
setSystemProperty(PID_KEY, new ApplicationPid().toString()); setSystemProperty(PID_KEY, new ApplicationPid().toString());
if (logFile != null) {
logFile.applyToSystemProperties();
}
} }
private void setSystemProperty(RelaxedPropertyResolver propertyResolver, private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
......
...@@ -35,6 +35,7 @@ import org.junit.rules.ExpectedException; ...@@ -35,6 +35,7 @@ import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem;
...@@ -441,6 +442,16 @@ public class LoggingApplicationListenerTests { ...@@ -441,6 +442,16 @@ public class LoggingApplicationListenerTests {
assertThat(System.getProperty("PID"), is(not(nullValue()))); assertThat(System.getProperty("PID"), is(not(nullValue())));
} }
@Test
public void logFilePropertiesCanReferenceSystemProperties() {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.file=target/${PID}.log");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
assertThat(System.getProperty("LOG_FILE"),
is(equalTo("target/" + new ApplicationPid().toString() + ".log")));
}
private boolean bridgeHandlerInstalled() { private boolean bridgeHandlerInstalled() {
Logger rootLogger = LogManager.getLogManager().getLogger(""); Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers(); Handler[] handlers = rootLogger.getHandlers();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment