diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java index 588f34c702e..9cce7b176bc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java @@ -42,34 +42,55 @@ public abstract class AbstractLoggingSystem extends LoggingSystem { @Override public void initialize(String configLocation, LogFile logFile) { if (StringUtils.hasLength(configLocation)) { - // Load a specific configuration - configLocation = SystemPropertyUtils.resolvePlaceholders(configLocation); - loadConfiguration(configLocation, logFile); + initializeWithSpecificConfig(configLocation, logFile); + return; } - else { - String selfInitializationConfig = getSelfInitializationConfig(); - if (selfInitializationConfig == null) { - // No self initialization has occurred, use defaults - loadDefaults(logFile); - } - else if (logFile != null) { - // Self initialization has occurred but the file has changed, reload - loadConfiguration(selfInitializationConfig, logFile); - } - else { - reinitialize(); - } + initializeWithConventions(logFile); + } + + private void initializeWithSpecificConfig(String configLocation, LogFile logFile) { + configLocation = SystemPropertyUtils.resolvePlaceholders(configLocation); + loadConfiguration(configLocation, logFile); + } + + private void initializeWithConventions(LogFile logFile) { + String config = getSelfInitializationConfig(); + if (config != null && logFile == null) { + // self initialization has occurred, reinitialize in case of property changes + reinitialize(); + return; } + if (config == null) { + config = getSpringInitializationConfig(); + } + if (config != null) { + loadConfiguration(config, logFile); + return; + } + loadDefaults(logFile); } /** * Return any self initialization config that has been applied. By default this method * checks {@link #getStandardConfigLocations()} and assumes that any file that exists * will have been applied. - * @return the self initialization config + * @return the self initialization configor {@code null} */ protected String getSelfInitializationConfig() { - for (String location : getStandardConfigLocations()) { + return findConfig(getStandardConfigLocations()); + } + + /** + * Return any spring specific initialization config that should be applied. By default + * this method checks {@link #getSpringConfigLocations()}. + * @return the spring initialization config or {@code null} + */ + protected String getSpringInitializationConfig() { + return findConfig(getSpringConfigLocations()); + } + + private String findConfig(String[] locations) { + for (String location : locations) { ClassPathResource resource = new ClassPathResource(location, this.classLoader); if (resource.exists()) { return "classpath:" + location; @@ -85,6 +106,23 @@ public abstract class AbstractLoggingSystem extends LoggingSystem { */ protected abstract String[] getStandardConfigLocations(); + /** + * Return the spring config locations for this system. By default this method returns + * a set of locations based on {@link #getStandardConfigLocations()}. + * @return the standard config locations + * @see #getSpringInitializationConfig() + */ + protected String[] getSpringConfigLocations() { + String[] locations = getStandardConfigLocations(); + for (int i = 0; i < locations.length; i++) { + String extension = StringUtils.getFilenameExtension(locations[i]); + locations[i] = locations[i].substring(0, + locations[i].length() - extension.length() - 1) + + "-spring." + extension; + } + return locations; + } + /** * Load sensible defaults for the logging system. * @param logFile the file to load or {@code null} if no log file is to be written diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java index 79f4f9a5221..507c9496a98 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java @@ -60,6 +60,10 @@ public abstract class AbstractLoggingSystemTests { System.clearProperty("PID"); } + protected final String[] getSpringConfigLocations(AbstractLoggingSystem system) { + return system.getSpringConfigLocations(); + } + protected final LogFile getLogFile(String file, String path) { return new LogFile(file, path); } diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index d48299e1e81..37f0344b7fe 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -161,6 +161,22 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertFalse(bridgeHandlerInstalled()); } + @Test + public void standardConfigLocations() throws Exception { + String[] locations = this.loggingSystem.getStandardConfigLocations(); + assertThat(locations, equalTo(new String[] { "logback-test.groovy", + "logback-test.xml", "logback.groovy", "logback.xml" })); + } + + @Test + public void springConfigLocations() throws Exception { + String[] locations = getSpringConfigLocations(this.loggingSystem); + assertThat(locations, + equalTo(new String[] { "logback-test-spring.groovy", + "logback-test-spring.xml", "logback-spring.groovy", + "logback-spring.xml" })); + } + private boolean bridgeHandlerInstalled() { java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger(""); Handler[] handlers = rootLogger.getHandlers();