Commit c4caf270 authored by Phillip Webb's avatar Phillip Webb

Defer devtools logging

Defer logging from devtools classes until the context is prepared.

Closes gh-14453
parent bc92becf
/*
* 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<DeferredLog, Class<?>> 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<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
synchronized (logs) {
logs.forEach((log, source) -> log.switchTo(source));
logs.clear();
}
}
}
}
......@@ -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<URL> {
private static final Log logger = LogFactory.getLog(ChangeableUrls.class);
private static final Log logger = DevToolsLogFactory.getLog(ChangeableUrls.class);
private final List<URL> urls;
......
......@@ -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;
......
......@@ -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=\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment