Add Spring specific logging configuration support
Update AbstractLoggingSystem to support convention based logging configuration specifically for Spring applications. Configurations with the `-spring` suffix will now be loaded automatically (for example `logback-spring.xml`). This change allows for custom log configurations without needing to reinitialize the logging system. When standard configurations are used there is no way to prevent early initialization. Fixes gh-2558
This commit is contained in:
parent
1f6ac52b96
commit
09eed727fe
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue