24
24
import java .lang .annotation .RetentionPolicy ;
25
25
import java .lang .annotation .Target ;
26
26
import java .net .ProtocolException ;
27
+ import java .util .ArrayList ;
27
28
import java .util .EnumSet ;
28
29
import java .util .LinkedHashMap ;
29
30
import java .util .List ;
30
31
import java .util .Map ;
31
32
import java .util .logging .Handler ;
32
33
import java .util .logging .Level ;
34
+ import java .util .stream .Stream ;
33
35
34
36
import com .fasterxml .jackson .databind .ObjectMapper ;
35
37
import org .apache .commons .logging .Log ;
38
40
import org .apache .logging .log4j .Logger ;
39
41
import org .apache .logging .log4j .core .LoggerContext ;
40
42
import org .apache .logging .log4j .core .config .Configuration ;
41
- import org .apache .logging .log4j .core .config .ConfigurationFactory ;
42
43
import org .apache .logging .log4j .core .config .LoggerConfig ;
43
44
import org .apache .logging .log4j .core .config .Reconfigurable ;
44
45
import org .apache .logging .log4j .core .config .composite .CompositeConfiguration ;
46
+ import org .apache .logging .log4j .core .config .json .JsonConfigurationFactory ;
45
47
import org .apache .logging .log4j .core .config .plugins .util .PluginRegistry ;
48
+ import org .apache .logging .log4j .core .config .properties .PropertiesConfigurationBuilder ;
49
+ import org .apache .logging .log4j .core .config .properties .PropertiesConfigurationFactory ;
46
50
import org .apache .logging .log4j .core .config .xml .XmlConfiguration ;
51
+ import org .apache .logging .log4j .core .config .yaml .YamlConfigurationFactory ;
47
52
import org .apache .logging .log4j .core .util .ShutdownCallbackRegistry ;
48
53
import org .apache .logging .log4j .jul .Log4jBridgeHandler ;
49
54
import org .apache .logging .log4j .status .StatusListener ;
53
58
import org .junit .jupiter .api .BeforeEach ;
54
59
import org .junit .jupiter .api .Test ;
55
60
import org .junit .jupiter .api .extension .ExtendWith ;
61
+ import org .junit .jupiter .params .ParameterizedTest ;
62
+ import org .junit .jupiter .params .provider .Arguments ;
63
+ import org .junit .jupiter .params .provider .MethodSource ;
56
64
import org .slf4j .MDC ;
57
65
58
66
import org .springframework .boot .logging .AbstractLoggingSystemTests ;
89
97
* @author Andy Wilkinson
90
98
* @author Ben Hale
91
99
* @author Madhura Bhave
100
+ * @author Piotr P. Karwasz
92
101
*/
93
102
@ ExtendWith (OutputCaptureExtension .class )
94
103
@ ClassPathExclusions ("logback-*.jar" )
@@ -105,6 +114,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
105
114
106
115
private Configuration configuration ;
107
116
117
+ private String contextName ;
118
+
108
119
@ BeforeEach
109
120
void setup () {
110
121
PluginRegistry .getInstance ().clear ();
@@ -115,6 +126,7 @@ void setup() {
115
126
this .configuration = loggerContext .getConfiguration ();
116
127
this .loggingSystem .cleanUp ();
117
128
this .logger = LogManager .getLogger (getClass ());
129
+ this .contextName = loggerContext .getName ();
118
130
}
119
131
120
132
@ AfterEach
@@ -293,54 +305,79 @@ void loggingThatUsesJulIsCaptured(CapturedOutput output) {
293
305
assertThat (output ).contains ("Hello world" );
294
306
}
295
307
296
- @ Test
297
- void configLocationsWithNoExtraDependencies () {
298
- assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2-test.properties" ,
299
- "log4j2-test.xml" , "log4j2.properties" , "log4j2.xml" );
300
- }
301
-
302
- @ Test
303
- void configLocationsWithJacksonDatabind () {
304
- this .loggingSystem .availableClasses (ObjectMapper .class .getName ());
305
- assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
306
- "log4j2-test.json" , "log4j2-test.jsn" , "log4j2-test.xml" , "log4j2.properties" , "log4j2.json" ,
307
- "log4j2.jsn" , "log4j2.xml" );
308
- }
309
-
310
- @ Test
311
- void configLocationsWithJacksonDataformatYaml () {
312
- this .loggingSystem .availableClasses ("com.fasterxml.jackson.dataformat.yaml.YAMLParser" );
313
- assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
314
- "log4j2-test.yaml" , "log4j2-test.yml" , "log4j2-test.xml" , "log4j2.properties" , "log4j2.yaml" ,
315
- "log4j2.yml" , "log4j2.xml" );
316
- }
317
-
318
- @ Test
319
- void configLocationsWithJacksonDatabindAndDataformatYaml () {
320
- this .loggingSystem .availableClasses ("com.fasterxml.jackson.dataformat.yaml.YAMLParser" ,
321
- ObjectMapper .class .getName ());
322
- assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
323
- "log4j2-test.yaml" , "log4j2-test.yml" , "log4j2-test.json" , "log4j2-test.jsn" , "log4j2-test.xml" ,
324
- "log4j2.properties" , "log4j2.yaml" , "log4j2.yml" , "log4j2.json" , "log4j2.jsn" , "log4j2.xml" );
308
+ static Stream <String > configLocationsWithConfigurationFileSystemProperty () {
309
+ return Stream .of ("log4j2.configurationFile" , "log4j.configuration.location" );
325
310
}
326
311
327
- @ Test
328
- void configLocationsWithConfigurationFileSystemProperty () {
329
- System .setProperty (ConfigurationFactory .CONFIGURATION_FILE_PROPERTY , "custom-log4j2.properties" );
312
+ @ ParameterizedTest
313
+ @ MethodSource
314
+ void configLocationsWithConfigurationFileSystemProperty (String propertyName ) {
315
+ System .setProperty (propertyName , "custom-log4j2.properties" );
330
316
try {
331
- assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
332
- "log4j2-test.xml" , "log4j2.properties" , "log4j2.xml" , "custom-log4j2.properties" );
317
+ assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("custom-log4j2.properties" ,
318
+ "log4j2-test" + this .contextName + ".xml" , "log4j2-test.xml" , "log4j2" + this .contextName + ".xml" ,
319
+ "log4j2.xml" );
333
320
}
334
321
finally {
335
- System .clearProperty (ConfigurationFactory . CONFIGURATION_FILE_PROPERTY );
322
+ System .clearProperty (propertyName );
336
323
}
337
324
}
338
325
326
+ static Stream <Arguments > standardConfigLocations () {
327
+ // For each configuration file format we make "available" to the
328
+ // Log4j2LoggingSystem:
329
+ // - The Log4j Core `ConfigurationFactory` class
330
+ // - The tree parser used internally by that configuration factory
331
+ return Stream .of (
332
+ // No classes, only XML
333
+ Arguments .of (List .of (), List .of (".xml" )),
334
+ // Log4j Core 2
335
+ Arguments .of (List .of (JsonConfigurationFactory .class .getName (), ObjectMapper .class .getName ()),
336
+ List .of (".json" , ".jsn" , ".xml" )),
337
+ Arguments .of (List .of (PropertiesConfigurationFactory .class .getName (),
338
+ PropertiesConfigurationBuilder .class .getName ()), List .of (".properties" , ".xml" )),
339
+ Arguments .of (List .of (YamlConfigurationFactory .class .getName (),
340
+ "com.fasterxml.jackson.dataformat.yaml.YAMLMapper" ), List .of (".yaml" , ".yml" , ".xml" )),
341
+ Arguments .of (List .of (JsonConfigurationFactory .class .getName (), ObjectMapper .class .getName (),
342
+ PropertiesConfigurationFactory .class .getName (), PropertiesConfigurationBuilder .class .getName (),
343
+ YamlConfigurationFactory .class .getName (), "com.fasterxml.jackson.dataformat.yaml.YAMLMapper" ),
344
+ List .of (".properties" , ".yaml" , ".yml" , ".json" , ".jsn" , ".xml" )),
345
+ // Log4j Core 3
346
+ Arguments .of (List .of (JsonConfigurationFactory .class .getName (),
347
+ "org.apache.logging.log4j.kit.json.JsonReader" ), List .of (".json" , ".jsn" , ".xml" )),
348
+ Arguments .of (List .of ("org.apache.logging.log4j.config.properties.JavaPropsConfigurationFactory" ,
349
+ "tools.jackson.dataformat.javaprop.JavaPropsMapper" ), List .of (".properties" , ".xml" )),
350
+ Arguments .of (List .of ("org.apache.logging.log4j.config.yaml.YamlConfigurationFactory" ,
351
+ "tools.jackson.dataformat.yaml.YAMLMapper" ), List .of (".yaml" , ".yml" , ".xml" )),
352
+ Arguments .of (
353
+ List .of (JsonConfigurationFactory .class .getName (),
354
+ "org.apache.logging.log4j.kit.json.JsonReader" ,
355
+ "org.apache.logging.log4j.config.properties.JavaPropsConfigurationFactory" ,
356
+ "tools.jackson.dataformat.javaprop.JavaPropsMapper" ,
357
+ "org.apache.logging.log4j.config.yaml.YamlConfigurationFactory" ,
358
+ "tools.jackson.dataformat.yaml.YAMLMapper" ),
359
+ List .of (".properties" , ".yaml" , ".yml" , ".json" , ".jsn" , ".xml" )));
360
+ }
361
+
362
+ @ ParameterizedTest
363
+ @ MethodSource
364
+ void standardConfigLocations (List <String > availableClasses , List <String > expectedSuffixes ) {
365
+ this .loggingSystem .availableClasses (availableClasses .toArray (new String [0 ]));
366
+ String [] locations = this .loggingSystem .getStandardConfigLocations ();
367
+ assertThat (locations ).hasSize (4 * expectedSuffixes .size ());
368
+ List <String > expected = new ArrayList <>();
369
+ expectedSuffixes .forEach (s -> expected .add ("log4j2-test" + this .contextName + s ));
370
+ expectedSuffixes .forEach (s -> expected .add ("log4j2-test" + s ));
371
+ expectedSuffixes .forEach (s -> expected .add ("log4j2" + this .contextName + s ));
372
+ expectedSuffixes .forEach (s -> expected .add ("log4j2" + s ));
373
+ assertThat (locations ).containsExactlyElementsOf (expected );
374
+ }
375
+
339
376
@ Test
340
377
void springConfigLocations () {
341
378
String [] locations = getSpringConfigLocations (this .loggingSystem );
342
- assertThat (locations ).containsExactly ("log4j2-test-spring.properties " , "log4j2-test-spring.xml" ,
343
- "log4j2-spring.properties " , "log4j2-spring.xml" );
379
+ assertThat (locations ).containsExactly ("log4j2-test" + contextName + " -spring.xml " , "log4j2-test-spring.xml" ,
380
+ "log4j2" + contextName + " -spring.xml " , "log4j2-spring.xml" );
344
381
}
345
382
346
383
@ Test
0 commit comments