40
40
import org .apache .logging .log4j .core .filter .DenyAllFilter ;
41
41
import org .apache .logging .log4j .core .util .NameUtil ;
42
42
import org .apache .logging .log4j .jul .Log4jBridgeHandler ;
43
- import org .apache .logging .log4j .spi .LoggerContextFactory ;
44
43
import org .apache .logging .log4j .status .StatusConsoleListener ;
45
44
import org .apache .logging .log4j .status .StatusLogger ;
46
45
import org .apache .logging .log4j .util .PropertiesUtil ;
@@ -110,9 +109,28 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
110
109
111
110
private final LoggerContext loggerContext ;
112
111
113
- private Log4J2LoggingSystem (ClassLoader classLoader , org .apache .logging .log4j .spi .LoggerContext loggerContext ) {
112
+ /**
113
+ * Create a new {@link Log4J2LoggingSystem} instance.
114
+ * @param classLoader the class loader to use.
115
+ * @param loggerContext the {@link LoggerContext} to use.
116
+ */
117
+ Log4J2LoggingSystem (ClassLoader classLoader , LoggerContext loggerContext ) {
114
118
super (classLoader );
115
- this .loggerContext = (LoggerContext ) loggerContext ;
119
+ this .loggerContext = loggerContext ;
120
+ }
121
+
122
+ /**
123
+ * Create a new {@link Log4J2LoggingSystem} instance.
124
+ * @param classLoader the class loader to use
125
+ * @return a new {@link Log4J2LoggingSystem} instance
126
+ * @throws IllegalStateException if Log4j Core is not the active Log4j API provider.
127
+ */
128
+ private static Log4J2LoggingSystem createLoggingSystem (ClassLoader classLoader ) {
129
+ org .apache .logging .log4j .spi .LoggerContext loggerContext = LogManager .getContext (classLoader , false );
130
+ if (loggerContext instanceof LoggerContext ) {
131
+ return new Log4J2LoggingSystem (classLoader , (LoggerContext ) loggerContext );
132
+ }
133
+ throw new IllegalStateException ("Log4j Core is not the active Log4j API provider" );
116
134
}
117
135
118
136
/**
@@ -136,7 +154,8 @@ protected String[] getStandardConfigLocations() {
136
154
ConfigurationFactory configurationFactory = ConfigurationFactory .getInstance ();
137
155
Configuration springConfiguration = configurationFactory .getConfiguration (getLoggerContext (), "-spring" , null ,
138
156
getClassLoader ());
139
- return getConfigLocation (springConfiguration );
157
+ String configLocation = getConfigLocation (springConfiguration );
158
+ return (configLocation != null && configLocation .contains ("-spring" )) ? configLocation : null ;
140
159
}
141
160
142
161
private @ Nullable String getConfigLocation (Configuration configuration ) {
@@ -460,7 +479,7 @@ public void cleanUp() {
460
479
return configuration .getLoggers ().get (name );
461
480
}
462
481
463
- private LoggerContext getLoggerContext () {
482
+ LoggerContext getLoggerContext () {
464
483
return this .loggerContext ;
465
484
}
466
485
@@ -496,20 +515,22 @@ protected String getDefaultLogCorrelationPattern() {
496
515
* {@link LoggingSystemFactory} that returns {@link Log4J2LoggingSystem} if possible.
497
516
*/
498
517
@ Order (0 )
499
- public static class Factory extends LogManager implements LoggingSystemFactory {
500
-
501
- private static final String FQCN = Factory .class .getName ();
518
+ public static class Factory implements LoggingSystemFactory {
502
519
503
520
private static final String LOG4J_CORE_CONTEXT_FACTORY = "org.apache.logging.log4j.core.impl.Log4jContextFactory" ;
504
521
522
+ private static final boolean PRESENT = ClassUtils .isPresent (LOG4J_CORE_CONTEXT_FACTORY ,
523
+ Factory .class .getClassLoader ());
524
+
505
525
@ Override
506
526
public @ Nullable LoggingSystem getLoggingSystem (ClassLoader classLoader ) {
507
- LoggerContextFactory contextFactory = getFactory ();
508
- // At the same time, we check that Log4j Core is present and that it is the
509
- // active
510
- // implementation.
511
- if (LOG4J_CORE_CONTEXT_FACTORY .equals (contextFactory .getClass ().getName ())) {
512
- return new Log4J2LoggingSystem (classLoader , contextFactory .getContext (FQCN , classLoader , null , false ));
527
+ if (PRESENT ) {
528
+ try {
529
+ return createLoggingSystem (classLoader );
530
+ }
531
+ catch (IllegalStateException ex ) {
532
+ // Log4j Core is not the active Log4j API provider
533
+ }
513
534
}
514
535
return null ;
515
536
}
0 commit comments