From 4eda91d6db9845a437276d204cd07f10ef50621c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 29 Feb 2016 12:32:47 +0000 Subject: [PATCH] Allow logging.file and logging.path to refer to logging system properties Closes gh-5274 --- .../boot/logging/LoggingApplicationListener.java | 11 +++++------ .../boot/logging/LoggingApplicationListenerTests.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 6122c5499f9..4c63db9403e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -247,16 +247,18 @@ public class LoggingApplicationListener implements GenericApplicationListener { */ protected void initialize(ConfigurableEnvironment environment, ClassLoader classLoader) { + setSystemProperties(environment); LogFile logFile = LogFile.get(environment); - setSystemProperties(environment, logFile); + if (logFile != null) { + logFile.applyToSystemProperties(); + } initializeEarlyLoggingLevel(environment); initializeSystem(environment, this.loggingSystem, logFile); initializeFinalLoggingLevels(environment, this.loggingSystem); registerShutdownHookIfNecessary(environment, this.loggingSystem); } - private void setSystemProperties(ConfigurableEnvironment environment, - LogFile logFile) { + private void setSystemProperties(ConfigurableEnvironment environment) { RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver( environment, "logging."); setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD, @@ -265,9 +267,6 @@ public class LoggingApplicationListener implements GenericApplicationListener { setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file"); setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level"); setSystemProperty(PID_KEY, new ApplicationPid().toString()); - if (logFile != null) { - logFile.applyToSystemProperties(); - } } private void setSystemProperty(RelaxedPropertyResolver propertyResolver, diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java index d7ef3c6e94a..3941793e93c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java @@ -35,6 +35,7 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.slf4j.bridge.SLF4JBridgeHandler; +import org.springframework.boot.ApplicationPid; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.logging.java.JavaLoggingSystem; @@ -441,6 +442,16 @@ public class LoggingApplicationListenerTests { 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() { Logger rootLogger = LogManager.getLogManager().getLogger(""); Handler[] handlers = rootLogger.getHandlers();