Reinstate devtools debug logging with opt-out

Reinstate `web` logging when devtools is in use, making use of the new
logging groups support. Devtools now also logs an `INFO` message
informing that properties defaults are offers an easy way to disable
them.

Closes gh-14450
This commit is contained in:
Phillip Webb
2018-09-12 15:57:19 -07:00
parent c4caf2705a
commit cef635d86c
3 changed files with 49 additions and 21 deletions

View File

@@ -20,7 +20,10 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
import org.springframework.boot.devtools.restart.Restarter;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
@@ -28,7 +31,6 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
/**
* {@link EnvironmentPostProcessor} to add properties that make sense when working at
@@ -42,33 +44,40 @@ import org.springframework.core.env.PropertySource;
@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {
private static final Log logger = DevToolsLogFactory
.getLog(DevToolsPropertyDefaultsPostProcessor.class);
private static final String ENABLED = "spring.devtools.add-properties";
private static final Map<String, Object> PROPERTIES;
static {
Map<String, Object> devToolsProperties = new HashMap<>();
devToolsProperties.put("spring.thymeleaf.cache", "false");
devToolsProperties.put("spring.freemarker.cache", "false");
devToolsProperties.put("spring.groovy.template.cache", "false");
devToolsProperties.put("spring.mustache.cache", "false");
devToolsProperties.put("server.servlet.session.persistent", "true");
devToolsProperties.put("spring.h2.console.enabled", "true");
devToolsProperties.put("spring.resources.cache.period", "0");
devToolsProperties.put("spring.resources.chain.cache", "false");
devToolsProperties.put("spring.template.provider.cache", "false");
devToolsProperties.put("spring.mvc.log-resolved-exception", "true");
devToolsProperties.put("server.error.include-stacktrace", "ALWAYS");
devToolsProperties.put("server.servlet.jsp.init-parameters.development", "true");
devToolsProperties.put("spring.reactor.stacktrace-mode.enabled", "true");
PROPERTIES = Collections.unmodifiableMap(devToolsProperties);
Map<String, Object> properties = new HashMap<>();
properties.put("spring.thymeleaf.cache", "false");
properties.put("spring.freemarker.cache", "false");
properties.put("spring.groovy.template.cache", "false");
properties.put("spring.mustache.cache", "false");
properties.put("server.servlet.session.persistent", "true");
properties.put("spring.h2.console.enabled", "true");
properties.put("spring.resources.cache.period", "0");
properties.put("spring.resources.chain.cache", "false");
properties.put("spring.template.provider.cache", "false");
properties.put("spring.mvc.log-resolved-exception", "true");
properties.put("server.error.include-stacktrace", "ALWAYS");
properties.put("server.servlet.jsp.init-parameters.development", "true");
properties.put("spring.reactor.stacktrace-mode.enabled", "true");
properties.put("logging.level.web", "debug");
PROPERTIES = Collections.unmodifiableMap(properties);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
if (isLocalApplication(environment) && canAddProperties(environment)) {
PropertySource<?> propertySource = new MapPropertySource("refresh",
PROPERTIES);
environment.getPropertySources().addLast(propertySource);
logger.info("Devtools property and logging defaults active! Set '" + ENABLED
+ "' to 'false' to disable");
environment.getPropertySources()
.addLast(new MapPropertySource("devtools", PROPERTIES));
}
}
@@ -77,7 +86,10 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
}
private boolean canAddProperties(Environment environment) {
return isRestarterInitialized() || isRemoteRestartEnabled(environment);
if (environment.getProperty(ENABLED, Boolean.class, true)) {
return isRestarterInitialized() || isRemoteRestartEnabled(environment);
}
return false;
}
private boolean isRestarterInitialized() {

View File

@@ -19,6 +19,12 @@
"reason": "Remote debug is no longer supported.",
"level": "error"
}
},
{
"name": "spring.devtools.add-properties",
"type": "java.lang.Boolean",
"description": "Whether to enable devtool property defaults.",
"defaultValue": true
}
]
}
}