From 1168d8fa742ce432536a983b4159c5c79b1e0beb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 3 Aug 2020 11:38:10 +0100 Subject: [PATCH] Reduce reflection in LoggingSystem to make it more Graal-friendly Closes gh-22594 --- .../boot/logging/LoggingSystem.java | 33 +++++++++------ ...ckAndLog4J2ExcludedLoggingSystemTests.java | 40 +++++++++++++++++++ .../boot/logging/LoggingSystemTests.java | 6 +++ 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java index d906e29336..1dc032c76f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java @@ -19,11 +19,13 @@ package org.springframework.boot.logging; import java.lang.reflect.Constructor; import java.util.Collections; import java.util.EnumSet; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Set; +import java.util.function.Function; +import org.springframework.boot.logging.java.JavaLoggingSystem; +import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem; +import org.springframework.boot.logging.logback.LogbackLoggingSystem; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -56,15 +58,24 @@ public abstract class LoggingSystem { */ public static final String ROOT_LOGGER_NAME = "ROOT"; - private static final Map SYSTEMS; + private static final Function SYSTEM_FACTORY; static { - Map systems = new LinkedHashMap<>(); - systems.put("ch.qos.logback.core.Appender", "org.springframework.boot.logging.logback.LogbackLoggingSystem"); - systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory", - "org.springframework.boot.logging.log4j2.Log4J2LoggingSystem"); - systems.put("java.util.logging.LogManager", "org.springframework.boot.logging.java.JavaLoggingSystem"); - SYSTEMS = Collections.unmodifiableMap(systems); + ClassLoader classLoader = LoggingSystem.class.getClassLoader(); + if (ClassUtils.isPresent("ch.qos.logback.core.Appender", classLoader)) { + SYSTEM_FACTORY = (cl) -> new LogbackLoggingSystem(cl); + } + else if (ClassUtils.isPresent("org.apache.logging.log4j.core.impl.Log4jContextFactory", classLoader)) { + SYSTEM_FACTORY = (cl) -> new Log4J2LoggingSystem(cl); + } + else if (ClassUtils.isPresent("java.util.logging.LogManager", classLoader)) { + SYSTEM_FACTORY = (cl) -> new JavaLoggingSystem(cl); + } + else { + SYSTEM_FACTORY = (cl) -> { + throw new IllegalStateException("No suitable logging system located"); + }; + } } /** @@ -155,9 +166,7 @@ public abstract class LoggingSystem { } return get(classLoader, loggingSystem); } - return SYSTEMS.entrySet().stream().filter((entry) -> ClassUtils.isPresent(entry.getKey(), classLoader)) - .map((entry) -> get(classLoader, entry.getValue())).findFirst() - .orElseThrow(() -> new IllegalStateException("No suitable logging system located")); + return SYSTEM_FACTORY.apply(classLoader); } private static LoggingSystem get(ClassLoader classLoader, String loggingSystemClass) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java new file mode 100644 index 0000000000..eef81f11a4 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2019 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 + * + * https://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.logging; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.logging.java.JavaLoggingSystem; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link LoggingSystem} when Logback is not on the classpath. + * + * @author Andy Wilkinson + */ +// Log4j2 is implicitly excluded due to LOG4J-2030 +@ClassPathExclusions("logback-*.jar") +class LogbackAndLog4J2ExcludedLoggingSystemTests { + + @Test + void whenLogbackAndLog4J2AreNotPresentJULIsTheLoggingSystem() { + assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java index 036c3fc2b2..f043fa57ee 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem; +import org.springframework.boot.logging.logback.LogbackLoggingSystem; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -36,6 +37,11 @@ class LoggingSystemTests { System.clearProperty(LoggingSystem.SYSTEM_PROPERTY); } + @Test + void logbackIsTheDefaultLoggingSystem() { + assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(LogbackLoggingSystem.class); + } + @Test void loggingSystemCanBeDisabled() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, LoggingSystem.NONE);