2121import java .util .Map ;
2222import org .apache .logging .log4j .Level ;
2323import org .apache .logging .log4j .LogManager ;
24+ import org .apache .logging .log4j .Logger ;
2425import org .apache .logging .log4j .core .LoggerContext ;
2526import org .apache .logging .log4j .core .config .Configuration ;
2627import org .apache .solr .SolrTestCaseJ4 ;
@@ -66,9 +67,16 @@ public void testLogLevelHandlerOutput() throws Exception {
6667
6768 final Configuration config = ctx .getConfiguration ();
6869
70+ // Create and hold strong references to loggers to prevent GC from removing them
71+ // during test execution (Log4j2 uses weak references internally)
72+ Logger parentLogger = LogManager .getLogger (PARENT_LOGGER_NAME );
73+ Logger aLogger = LogManager .getLogger (A_LOGGER_NAME );
74+ Logger bLogger = LogManager .getLogger (B_LOGGER_NAME );
75+ Logger bxLogger = LogManager .getLogger (BX_LOGGER_NAME );
76+
6977 { // sanity check our setup...
7078
71- // did anybody break the anotations ?
79+ // did anybody break the annotations ?
7280 assertNotNull (this .getClass ().getAnnotation (LogLevel .class ));
7381 final String annotationConfig = this .getClass ().getAnnotation (LogLevel .class ).value ();
7482 assertTrue ("WTF: " + annotationConfig , annotationConfig .startsWith (PARENT_LOGGER_NAME ));
@@ -88,11 +96,11 @@ public void testLogLevelHandlerOutput() throws Exception {
8896 config .getLoggerConfig (logger ));
8997 }
9098
91- // Either explicit, or inherited, effictive logger values...
92- for ( String logger :
93- Arrays . asList ( PARENT_LOGGER_NAME , A_LOGGER_NAME , B_LOGGER_NAME , BX_LOGGER_NAME )) {
94- assertEquals (Level .DEBUG , LogManager . getLogger ( logger ) .getLevel ());
95- }
99+ // Either explicit, or inherited, effective logger values...
100+ assertEquals ( Level . DEBUG , parentLogger . getLevel ());
101+ assertEquals ( Level . DEBUG , aLogger . getLevel ());
102+ assertEquals (Level .DEBUG , bLogger .getLevel ());
103+ assertEquals ( Level . DEBUG , bxLogger . getLevel ());
96104 }
97105
98106 SolrClient client = new EmbeddedSolrServer (h .getCore ());
@@ -130,18 +138,18 @@ public void testLogLevelHandlerOutput() throws Exception {
130138 assertLoggerLevel (updatedLoggerLevel , B_LOGGER_NAME , "TRACE" , true );
131139 assertLoggerLevel (updatedLoggerLevel , BX_LOGGER_NAME , "TRACE" , false );
132140
133- // check directly with log4j what it's (updated) config has...
141+ // check directly with log4j what its (updated) config has...
134142 assertEquals (Level .DEBUG , config .getLoggerConfig (PARENT_LOGGER_NAME ).getExplicitLevel ());
135143 assertEquals (Level .TRACE , config .getLoggerConfig (B_LOGGER_NAME ).getExplicitLevel ());
136144 assertEquals (
137145 "Unexpected config for BX ... expected B's config" ,
138146 config .getLoggerConfig (B_LOGGER_NAME ),
139147 config .getLoggerConfig (BX_LOGGER_NAME ));
140- // ...and what it's effective values
141- assertEquals (Level .DEBUG , LogManager . getLogger ( PARENT_LOGGER_NAME ) .getLevel ());
142- assertEquals (Level .DEBUG , LogManager . getLogger ( A_LOGGER_NAME ) .getLevel ());
143- assertEquals (Level .TRACE , LogManager . getLogger ( B_LOGGER_NAME ) .getLevel ());
144- assertEquals (Level .TRACE , LogManager . getLogger ( BX_LOGGER_NAME ) .getLevel ());
148+ // ...and what its effective values are
149+ assertEquals (Level .DEBUG , parentLogger .getLevel ());
150+ assertEquals (Level .DEBUG , aLogger .getLevel ());
151+ assertEquals (Level .TRACE , bLogger .getLevel ());
152+ assertEquals (Level .TRACE , bxLogger .getLevel ());
145153 }
146154
147155 { // UNSET
@@ -161,7 +169,7 @@ public void testLogLevelHandlerOutput() throws Exception {
161169 assertLoggerLevel (removedLoggerLevel , B_LOGGER_NAME , "DEBUG" , false );
162170 assertLoggerLevel (removedLoggerLevel , BX_LOGGER_NAME , "DEBUG" , false );
163171
164- // check directly with log4j what it's (updated) config has...
172+ // check directly with log4j what its (updated) config has...
165173 //
166174 // NOTE: LoggingHandler must not actually "remove" the LoggerConfig for B on 'unset'
167175 // (it might have already been defined in log4j's original config for some other reason,
@@ -176,11 +184,11 @@ public void testLogLevelHandlerOutput() throws Exception {
176184 "Unexpected config for BX ... expected B's config" ,
177185 config .getLoggerConfig (B_LOGGER_NAME ),
178186 config .getLoggerConfig (BX_LOGGER_NAME ));
179- // ...and what it's effective values
180- assertEquals (Level .DEBUG , LogManager . getLogger ( PARENT_LOGGER_NAME ) .getLevel ());
181- assertEquals (Level .DEBUG , LogManager . getLogger ( A_LOGGER_NAME ) .getLevel ());
182- assertEquals (Level .DEBUG , LogManager . getLogger ( B_LOGGER_NAME ) .getLevel ());
183- assertEquals (Level .DEBUG , LogManager . getLogger ( BX_LOGGER_NAME ) .getLevel ());
187+ // ...and what its effective values are
188+ assertEquals (Level .DEBUG , parentLogger .getLevel ());
189+ assertEquals (Level .DEBUG , aLogger .getLevel ());
190+ assertEquals (Level .DEBUG , bLogger .getLevel ());
191+ assertEquals (Level .DEBUG , bxLogger .getLevel ());
184192 }
185193 }
186194
0 commit comments