Skip to content

Commit 04366f4

Browse files
committed
Update LogAdapter to allow build-time code removal
Allow for example to remove those classes and 90 related methods when Logback is used: - org.apache.commons.logging.LogAdapter$JavaUtilAdapter - org.apache.commons.logging.LogAdapter$JavaUtilLog - org.apache.commons.logging.LogAdapter$LocationResolvingLogRecord - org.apache.commons.logging.LogAdapter$Log4jAdapter - org.apache.commons.logging.LogAdapter$Log4jLog - org.apache.commons.logging.LogAdapter$LogApi - org.apache.logging.log4j.message.ObjectMessage - org.apache.logging.log4j.message.ReusableObjectMessage - org.apache.logging.log4j.simple.SimpleLoggerContext - org.apache.logging.log4j.simple.SimpleLoggerContextFactory Closes gh-29506
1 parent 2a2c679 commit 04366f4

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

spring-core/src/main/java/org/springframework/aot/nativex/feature/PreComputeFieldFeature.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class PreComputeFieldFeature implements Feature {
3636
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#imageCode")),
3737
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*Present"),
3838
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*PRESENT"),
39-
Pattern.compile(Pattern.quote("reactor.") + ".*#.*Available")
39+
Pattern.compile(Pattern.quote("reactor.") + ".*#.*Available"),
40+
Pattern.compile(Pattern.quote("org.apache.commons.logging.LogAdapter") + "#.*Present")
4041
};
4142

4243
private final ThrowawayClassLoader throwawayClassLoader = new ThrowawayClassLoader(PreComputeFieldFeature.class.getClassLoader());

spring-jcl/src/main/java/org/apache/commons/logging/LogAdapter.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.logging;
1818

1919
import java.io.Serializable;
20+
import java.util.function.Function;
2021
import java.util.logging.LogRecord;
2122

2223
import org.apache.logging.log4j.Level;
@@ -32,45 +33,52 @@
3233
* Detects the presence of Log4j 2.x / SLF4J, falling back to {@code java.util.logging}.
3334
*
3435
* @author Juergen Hoeller
36+
* @author Sebastien Deleuze
3537
* @since 5.1
3638
*/
3739
final class LogAdapter {
3840

39-
private static final String LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger";
41+
private static final boolean log4jSpiPresent = isPresent("org.apache.logging.log4j.spi.ExtendedLogger");
4042

41-
private static final String LOG4J_SLF4J_PROVIDER = "org.apache.logging.slf4j.SLF4JProvider";
43+
private static final boolean log4jSlf4jProviderPresent = isPresent("org.apache.logging.slf4j.SLF4JProvider");
4244

43-
private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger";
45+
private static final boolean slf4jSpiPresent = isPresent("org.slf4j.spi.LocationAwareLogger");
4446

45-
private static final String SLF4J_API = "org.slf4j.Logger";
47+
private static final boolean slf4jApiPresent = isPresent("org.slf4j.Logger");
4648

4749

48-
private static final LogApi logApi;
50+
private static final Function<String, Log> createLog;
4951

5052
static {
51-
if (isPresent(LOG4J_SPI)) {
52-
if (isPresent(LOG4J_SLF4J_PROVIDER) && isPresent(SLF4J_SPI)) {
53+
if (log4jSpiPresent) {
54+
if (log4jSlf4jProviderPresent && slf4jSpiPresent) {
5355
// log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;
5456
// however, we still prefer Log4j over the plain SLF4J API since
5557
// the latter does not have location awareness support.
56-
logApi = LogApi.SLF4J_LAL;
58+
createLog = Slf4jAdapter::createLocationAwareLog;
5759
}
5860
else {
5961
// Use Log4j 2.x directly, including location awareness support
60-
logApi = LogApi.LOG4J;
62+
createLog = Log4jAdapter::createLog;
6163
}
6264
}
63-
else if (isPresent(SLF4J_SPI)) {
65+
else if (slf4jSpiPresent) {
6466
// Full SLF4J SPI including location awareness support
65-
logApi = LogApi.SLF4J_LAL;
67+
createLog = Slf4jAdapter::createLocationAwareLog;
6668
}
67-
else if (isPresent(SLF4J_API)) {
69+
else if (slf4jApiPresent) {
6870
// Minimal SLF4J API without location awareness support
69-
logApi = LogApi.SLF4J;
71+
createLog = Slf4jAdapter::createLog;
7072
}
7173
else {
7274
// java.util.logging as default
73-
logApi = LogApi.JUL;
75+
// Defensively use lazy-initializing adapter class here as well since the
76+
// java.logging module is not present by default on JDK 9. We are requiring
77+
// its presence if neither Log4j nor SLF4J is available; however, in the
78+
// case of Log4j or SLF4J, we are trying to prevent early initialization
79+
// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly
80+
// trying to parse the bytecode for all the cases of this switch clause.
81+
createLog = JavaUtilAdapter::createLog;
7482
}
7583
}
7684

@@ -84,19 +92,7 @@ private LogAdapter() {
8492
* @param name the logger name
8593
*/
8694
public static Log createLog(String name) {
87-
return switch (logApi) {
88-
case LOG4J -> Log4jAdapter.createLog(name);
89-
case SLF4J_LAL -> Slf4jAdapter.createLocationAwareLog(name);
90-
case SLF4J -> Slf4jAdapter.createLog(name);
91-
default ->
92-
// Defensively use lazy-initializing adapter class here as well since the
93-
// java.logging module is not present by default on JDK 9. We are requiring
94-
// its presence if neither Log4j nor SLF4J is available; however, in the
95-
// case of Log4j or SLF4J, we are trying to prevent early initialization
96-
// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly
97-
// trying to parse the bytecode for all the cases of this switch clause.
98-
JavaUtilAdapter.createLog(name);
99-
};
95+
return createLog.apply(name);
10096
}
10197

10298
private static boolean isPresent(String className) {
@@ -110,9 +106,6 @@ private static boolean isPresent(String className) {
110106
}
111107

112108

113-
private enum LogApi {LOG4J, SLF4J_LAL, SLF4J, JUL}
114-
115-
116109
private static class Log4jAdapter {
117110

118111
public static Log createLog(String name) {

0 commit comments

Comments
 (0)