From 4940ca37ebea83bf618b9b5c082462a2a7aa2a21 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 3 Jan 2015 12:59:00 -0800 Subject: [PATCH] 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 --- .../boot/logging/LoggingSystem.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java index 79f5bc02e9a..50e46551bcc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java @@ -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 SYSTEMS; static { Map systems = new LinkedHashMap(); 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 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); + } + } + }