Skip to content

Commit c4caf27

Browse files
committed
Defer devtools logging
Defer logging from devtools classes until the context is prepared. Closes gh-14453
1 parent bc92bec commit c4caf27

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2018-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.devtools.logger;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.context.event.ApplicationPreparedEvent;
23+
import org.springframework.boot.logging.DeferredLog;
24+
import org.springframework.context.ApplicationListener;
25+
import org.springframework.data.domain.AbstractPageRequest;
26+
27+
/**
28+
* Devtools deferred logging support.
29+
*
30+
* @author Phillip Webb
31+
* @since 2.1.0
32+
*/
33+
public final class DevToolsLogFactory {
34+
35+
private static final Map<DeferredLog, Class<?>> logs = new LinkedHashMap<>();
36+
37+
private DevToolsLogFactory() {
38+
}
39+
40+
/**
41+
* Get a {@link DeferredLog} instance for the specified source that will be
42+
* automatically {@link DeferredLog#switchTo(Class) switched} then the
43+
* {@link AbstractPageRequest context is prepared}.
44+
* @param source the source for logging
45+
* @return a {@link DeferredLog} instance
46+
*/
47+
public static DeferredLog getLog(Class<?> source) {
48+
synchronized (logs) {
49+
DeferredLog log = new DeferredLog();
50+
logs.put(log, source);
51+
return log;
52+
}
53+
}
54+
55+
/**
56+
* Listener used to log and switch when the context is ready.
57+
*/
58+
static class Listener implements ApplicationListener<ApplicationPreparedEvent> {
59+
60+
@Override
61+
public void onApplicationEvent(ApplicationPreparedEvent event) {
62+
synchronized (logs) {
63+
logs.forEach((log, source) -> log.switchTo(source));
64+
logs.clear();
65+
}
66+
}
67+
68+
}
69+
70+
}

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import java.util.stream.Stream;
3434

3535
import org.apache.commons.logging.Log;
36-
import org.apache.commons.logging.LogFactory;
3736

37+
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
3838
import org.springframework.boot.devtools.settings.DevToolsSettings;
3939
import org.springframework.util.StringUtils;
4040

@@ -46,7 +46,7 @@
4646
*/
4747
final class ChangeableUrls implements Iterable<URL> {
4848

49-
private static final Log logger = LogFactory.getLog(ChangeableUrls.class);
49+
private static final Log logger = DevToolsLogFactory.getLog(ChangeableUrls.class);
5050

5151
private final List<URL> urls;
5252

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/settings/DevToolsSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import java.util.regex.Pattern;
2626

2727
import org.apache.commons.logging.Log;
28-
import org.apache.commons.logging.LogFactory;
2928

29+
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
3030
import org.springframework.core.io.UrlResource;
3131
import org.springframework.core.io.support.PropertiesLoaderUtils;
3232

@@ -43,7 +43,7 @@ public class DevToolsSettings {
4343
*/
4444
public static final String SETTINGS_RESOURCE_LOCATION = "META-INF/spring-devtools.properties";
4545

46-
private static final Log logger = LogFactory.getLog(DevToolsSettings.class);
46+
private static final Log logger = DevToolsLogFactory.getLog(DevToolsSettings.class);
4747

4848
private static DevToolsSettings settings;
4949

spring-boot-project/spring-boot-devtools/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ org.springframework.boot.devtools.restart.RestartScopeInitializer
44

55
# Application Listeners
66
org.springframework.context.ApplicationListener=\
7-
org.springframework.boot.devtools.restart.RestartApplicationListener
7+
org.springframework.boot.devtools.restart.RestartApplicationListener,\
8+
org.springframework.boot.devtools.logger.DevToolsLogFactory.Listener
89

910
# Auto Configure
1011
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

0 commit comments

Comments
 (0)