From a26ae0a303d37f29ecd51e095f25f90c933494eb Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 28 Jan 2014 13:24:14 +0000 Subject: [PATCH] Re-initialize logging if Environment changed settings Fixes gh-273 --- .../listener/LoggingApplicationListener.java | 6 +++++ .../LoggingApplicationListenerTests.java | 22 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java index c2682afa2e..7f0c6fc52d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/LoggingApplicationListener.java @@ -153,16 +153,22 @@ public class LoggingApplicationListener implements SmartApplicationListener { } } + boolean environmentChanged = false; for (Map.Entry mapping : ENVIRONMENT_SYSTEM_PROPERTY_MAPPING .entrySet()) { if (environment.containsProperty(mapping.getKey())) { System.setProperty(mapping.getValue(), environment.getProperty(mapping.getKey())); + environmentChanged = true; } } LoggingSystem system = LoggingSystem.get(classLoader); + if (environmentChanged) { + // Re-initialize the defaults in case the Environment changed + system.beforeInitialize(); + } // User specified configuration if (environment.containsProperty("logging.config")) { String value = environment.getProperty("logging.config"); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java index 8ceaccdf9a..503ba8c377 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/LoggingApplicationListenerTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.context.listener; +import java.io.File; import java.io.IOException; import java.util.logging.LogManager; @@ -71,6 +72,7 @@ public class LoggingApplicationListenerTests { JavaLoggingSystem.class.getResourceAsStream("logging.properties")); this.initializer.onApplicationEvent(new SpringApplicationStartEvent( new SpringApplication(), NO_ARGS)); + new File("target/foo.log").delete(); } @After @@ -119,25 +121,37 @@ public class LoggingApplicationListenerTests { public void testAddLogFileProperty() { EnvironmentTestUtils.addEnvironment(this.context, "logging.config: classpath:logback-nondefault.xml", - "logging.file: foo.log"); + "logging.file: target/foo.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); logger.info("Hello world"); 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 public void testAddLogPathProperty() { 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.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); logger.info("Hello world"); 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