diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/logger/DevToolsLogFactory.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/logger/DevToolsLogFactory.java new file mode 100644 index 00000000000..387160aaf1b --- /dev/null +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/logger/DevToolsLogFactory.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.devtools.logger; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.boot.logging.DeferredLog; +import org.springframework.context.ApplicationListener; +import org.springframework.data.domain.AbstractPageRequest; + +/** + * Devtools deferred logging support. + * + * @author Phillip Webb + * @since 2.1.0 + */ +public final class DevToolsLogFactory { + + private static final Map> logs = new LinkedHashMap<>(); + + private DevToolsLogFactory() { + } + + /** + * Get a {@link DeferredLog} instance for the specified source that will be + * automatically {@link DeferredLog#switchTo(Class) switched} then the + * {@link AbstractPageRequest context is prepared}. + * @param source the source for logging + * @return a {@link DeferredLog} instance + */ + public static DeferredLog getLog(Class source) { + synchronized (logs) { + DeferredLog log = new DeferredLog(); + logs.put(log, source); + return log; + } + } + + /** + * Listener used to log and switch when the context is ready. + */ + static class Listener implements ApplicationListener { + + @Override + public void onApplicationEvent(ApplicationPreparedEvent event) { + synchronized (logs) { + logs.forEach((log, source) -> log.switchTo(source)); + logs.clear(); + } + } + + } + +} diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java index 5b9f3960e43..3e91e64aa83 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java @@ -33,8 +33,8 @@ import java.util.jar.Manifest; import java.util.stream.Stream; import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.springframework.boot.devtools.logger.DevToolsLogFactory; import org.springframework.boot.devtools.settings.DevToolsSettings; import org.springframework.util.StringUtils; @@ -46,7 +46,7 @@ import org.springframework.util.StringUtils; */ final class ChangeableUrls implements Iterable { - private static final Log logger = LogFactory.getLog(ChangeableUrls.class); + private static final Log logger = DevToolsLogFactory.getLog(ChangeableUrls.class); private final List urls; diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/settings/DevToolsSettings.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/settings/DevToolsSettings.java index e21cacfa16f..ea0ab9f1a9f 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/settings/DevToolsSettings.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/settings/DevToolsSettings.java @@ -25,8 +25,8 @@ import java.util.Map; import java.util.regex.Pattern; import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.springframework.boot.devtools.logger.DevToolsLogFactory; import org.springframework.core.io.UrlResource; import org.springframework.core.io.support.PropertiesLoaderUtils; @@ -43,7 +43,7 @@ public class DevToolsSettings { */ public static final String SETTINGS_RESOURCE_LOCATION = "META-INF/spring-devtools.properties"; - private static final Log logger = LogFactory.getLog(DevToolsSettings.class); + private static final Log logger = DevToolsLogFactory.getLog(DevToolsSettings.class); private static DevToolsSettings settings; diff --git a/spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories index 232bf112d77..958c3dfa63c 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories @@ -4,7 +4,8 @@ org.springframework.boot.devtools.restart.RestartScopeInitializer # Application Listeners org.springframework.context.ApplicationListener=\ -org.springframework.boot.devtools.restart.RestartApplicationListener +org.springframework.boot.devtools.restart.RestartApplicationListener,\ +org.springframework.boot.devtools.logger.DevToolsLogFactory.Listener # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\