2424import  java .lang .annotation .RetentionPolicy ;
2525import  java .lang .annotation .Target ;
2626import  java .net .ProtocolException ;
27+ import  java .util .ArrayList ;
2728import  java .util .EnumSet ;
2829import  java .util .LinkedHashMap ;
2930import  java .util .List ;
3031import  java .util .Map ;
3132import  java .util .logging .Handler ;
3233import  java .util .logging .Level ;
34+ import  java .util .stream .Stream ;
3335
3436import  com .fasterxml .jackson .databind .ObjectMapper ;
3537import  org .apache .commons .logging .Log ;
3840import  org .apache .logging .log4j .Logger ;
3941import  org .apache .logging .log4j .core .LoggerContext ;
4042import  org .apache .logging .log4j .core .config .Configuration ;
41- import  org .apache .logging .log4j .core .config .ConfigurationFactory ;
4243import  org .apache .logging .log4j .core .config .LoggerConfig ;
4344import  org .apache .logging .log4j .core .config .Reconfigurable ;
4445import  org .apache .logging .log4j .core .config .composite .CompositeConfiguration ;
46+ import  org .apache .logging .log4j .core .config .json .JsonConfigurationFactory ;
4547import  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 ;
4650import  org .apache .logging .log4j .core .config .xml .XmlConfiguration ;
51+ import  org .apache .logging .log4j .core .config .yaml .YamlConfigurationFactory ;
4752import  org .apache .logging .log4j .core .util .ShutdownCallbackRegistry ;
4853import  org .apache .logging .log4j .jul .Log4jBridgeHandler ;
4954import  org .apache .logging .log4j .status .StatusListener ;
5358import  org .junit .jupiter .api .BeforeEach ;
5459import  org .junit .jupiter .api .Test ;
5560import  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 ;
5664import  org .slf4j .MDC ;
5765
5866import  org .springframework .boot .logging .AbstractLoggingSystemTests ;
8997 * @author Andy Wilkinson 
9098 * @author Ben Hale 
9199 * @author Madhura Bhave 
100+  * @author Piotr P. Karwasz 
92101 */ 
93102@ ExtendWith (OutputCaptureExtension .class )
94103@ ClassPathExclusions ("logback-*.jar" )
@@ -105,6 +114,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
105114
106115	private  Configuration  configuration ;
107116
117+ 	private  String  contextName ;
118+ 
108119	@ BeforeEach 
109120	void  setup () {
110121		PluginRegistry .getInstance ().clear ();
@@ -115,6 +126,7 @@ void setup() {
115126		this .configuration  = loggerContext .getConfiguration ();
116127		this .loggingSystem .cleanUp ();
117128		this .logger  = LogManager .getLogger (getClass ());
129+ 		this .contextName  = loggerContext .getName ();
118130	}
119131
120132	@ AfterEach 
@@ -293,54 +305,79 @@ void loggingThatUsesJulIsCaptured(CapturedOutput output) {
293305		assertThat (output ).contains ("Hello world" );
294306	}
295307
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" );
325310	}
326311
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" );
330316		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" );
333320		}
334321		finally  {
335- 			System .clearProperty (ConfigurationFactory . CONFIGURATION_FILE_PROPERTY );
322+ 			System .clearProperty (propertyName );
336323		}
337324	}
338325
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+ 
339376	@ Test 
340377	void  springConfigLocations () {
341378		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" );
344381	}
345382
346383	@ Test 
0 commit comments