Reduce reflection in LoggingSystem to make it more Graal-friendly
Closes gh-22594
This commit is contained in:
		
							parent
							
								
									989fc36528
								
							
						
					
					
						commit
						1168d8fa74
					
				|  | @ -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<String, String> SYSTEMS; | ||||
| 	private static final Function<ClassLoader, LoggingSystem> SYSTEM_FACTORY; | ||||
| 
 | ||||
| 	static { | ||||
| 		Map<String, String> 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) { | ||||
|  |  | |||
|  | @ -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); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -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); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue