17
17
package org .apache .commons .logging ;
18
18
19
19
import java .io .Serializable ;
20
+ import java .util .function .Function ;
20
21
import java .util .logging .LogRecord ;
21
22
22
23
import org .apache .logging .log4j .Level ;
32
33
* Detects the presence of Log4j 2.x / SLF4J, falling back to {@code java.util.logging}.
33
34
*
34
35
* @author Juergen Hoeller
36
+ * @author Sebastien Deleuze
35
37
* @since 5.1
36
38
*/
37
39
final class LogAdapter {
38
40
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" ) ;
40
42
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" ) ;
42
44
43
- private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger" ;
45
+ private static final boolean slf4jSpiPresent = isPresent ( "org.slf4j.spi.LocationAwareLogger" ) ;
44
46
45
- private static final String SLF4J_API = "org.slf4j.Logger" ;
47
+ private static final boolean slf4jApiPresent = isPresent ( "org.slf4j.Logger" ) ;
46
48
47
49
48
- private static final LogApi logApi ;
50
+ private static final Function < String , Log > createLog ;
49
51
50
52
static {
51
- if (isPresent ( LOG4J_SPI ) ) {
52
- if (isPresent ( LOG4J_SLF4J_PROVIDER ) && isPresent ( SLF4J_SPI ) ) {
53
+ if (log4jSpiPresent ) {
54
+ if (log4jSlf4jProviderPresent && slf4jSpiPresent ) {
53
55
// log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;
54
56
// however, we still prefer Log4j over the plain SLF4J API since
55
57
// the latter does not have location awareness support.
56
- logApi = LogApi . SLF4J_LAL ;
58
+ createLog = Slf4jAdapter :: createLocationAwareLog ;
57
59
}
58
60
else {
59
61
// Use Log4j 2.x directly, including location awareness support
60
- logApi = LogApi . LOG4J ;
62
+ createLog = Log4jAdapter :: createLog ;
61
63
}
62
64
}
63
- else if (isPresent ( SLF4J_SPI ) ) {
65
+ else if (slf4jSpiPresent ) {
64
66
// Full SLF4J SPI including location awareness support
65
- logApi = LogApi . SLF4J_LAL ;
67
+ createLog = Slf4jAdapter :: createLocationAwareLog ;
66
68
}
67
- else if (isPresent ( SLF4J_API ) ) {
69
+ else if (slf4jApiPresent ) {
68
70
// Minimal SLF4J API without location awareness support
69
- logApi = LogApi . SLF4J ;
71
+ createLog = Slf4jAdapter :: createLog ;
70
72
}
71
73
else {
72
74
// 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 ;
74
82
}
75
83
}
76
84
@@ -84,19 +92,7 @@ private LogAdapter() {
84
92
* @param name the logger name
85
93
*/
86
94
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 );
100
96
}
101
97
102
98
private static boolean isPresent (String className ) {
@@ -110,9 +106,6 @@ private static boolean isPresent(String className) {
110
106
}
111
107
112
108
113
- private enum LogApi {LOG4J , SLF4J_LAL , SLF4J , JUL }
114
-
115
-
116
109
private static class Log4jAdapter {
117
110
118
111
public static Log createLog (String name ) {
0 commit comments