Move Log4J2LoggingSystem above Log4JLoggingSystem

Change the LoggingSystem load order so that Log4J2 has a higher priority
than Log4J. Also add system property support to allow a specific system
to be used.

Fixes gh-2274
This commit is contained in:
Phillip Webb 2015-01-03 12:59:00 -08:00
parent f369a72264
commit 4940ca37eb
1 changed files with 24 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Common abstraction over logging systems.
@ -30,15 +31,20 @@ import org.springframework.util.ClassUtils;
*/
public abstract class LoggingSystem {
/**
* A System property that can be used to indicate the {@link LoggingSystem} to use.
*/
public static final String SYSTEM_PROPERTY = LoggingSystem.class.getName();
private static final Map<String, String> SYSTEMS;
static {
Map<String, String> systems = new LinkedHashMap<String, String>();
systems.put("ch.qos.logback.core.Appender",
"org.springframework.boot.logging.logback.LogbackLoggingSystem");
systems.put("org.apache.log4j.PropertyConfigurator",
"org.springframework.boot.logging.log4j.Log4JLoggingSystem");
systems.put("org.apache.logging.log4j.LogManager",
"org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");
systems.put("org.apache.log4j.PropertyConfigurator",
"org.springframework.boot.logging.log4j.Log4JLoggingSystem");
systems.put("java.util.logging.LogManager",
"org.springframework.boot.logging.java.JavaLoggingSystem");
SYSTEMS = Collections.unmodifiableMap(systems);
@ -73,20 +79,27 @@ public abstract class LoggingSystem {
* @return The logging system
*/
public static LoggingSystem get(ClassLoader classLoader) {
String loggingSystem = System.getProperty(SYSTEM_PROPERTY);
if (StringUtils.hasLength(loggingSystem)) {
return get(classLoader, loggingSystem);
}
for (Map.Entry<String, String> entry : SYSTEMS.entrySet()) {
if (ClassUtils.isPresent(entry.getKey(), classLoader)) {
try {
Class<?> systemClass = ClassUtils.forName(entry.getValue(),
classLoader);
return (LoggingSystem) systemClass.getConstructor(ClassLoader.class)
.newInstance(classLoader);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
return get(classLoader, entry.getValue());
}
}
throw new IllegalStateException("No suitable logging system located");
}
private static LoggingSystem get(ClassLoader classLoader, String loggingSystemClass) {
try {
Class<?> systemClass = ClassUtils.forName(loggingSystemClass, classLoader);
return (LoggingSystem) systemClass.getConstructor(ClassLoader.class)
.newInstance(classLoader);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}