42
42
import org .springframework .boot .logging .LoggingSystemProperties ;
43
43
import org .springframework .boot .testsupport .system .CapturedOutput ;
44
44
import org .springframework .boot .testsupport .system .OutputCaptureExtension ;
45
+ import org .springframework .util .ClassUtils ;
45
46
import org .springframework .util .StringUtils ;
46
47
47
48
import static org .assertj .core .api .Assertions .assertThat ;
59
60
* @author Phillip Webb
60
61
* @author Andy Wilkinson
61
62
* @author Ben Hale
63
+ * @author Madhura Bhave
62
64
*/
63
65
@ ExtendWith (OutputCaptureExtension .class )
64
66
class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -101,7 +103,8 @@ void noFile(CapturedOutput output) {
101
103
void withFile (CapturedOutput output ) {
102
104
this .loggingSystem .beforeInitialize ();
103
105
this .logger .info ("Hidden" );
104
- this .loggingSystem .initialize (null , null , getLogFile (null , tmpDir ()));
106
+ this .loggingSystem .initialize (null , getRelativeClasspathLocation ("log4j2-file.xml" ),
107
+ getLogFile (null , tmpDir ()));
105
108
this .logger .info ("Hello world" );
106
109
Configuration configuration = this .loggingSystem .getConfiguration ();
107
110
assertThat (output ).contains ("Hello world" ).doesNotContain ("Hidden" );
@@ -197,39 +200,47 @@ void loggingThatUsesJulIsCaptured(CapturedOutput output) {
197
200
198
201
@ Test
199
202
void configLocationsWithNoExtraDependencies () {
200
- assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2.properties" , "log4j2.xml" );
203
+ assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2-test.properties" ,
204
+ "log4j2-test.xml" , "log4j2.properties" , "log4j2.xml" );
201
205
}
202
206
203
207
@ Test
204
208
void configLocationsWithJacksonDatabind () {
205
209
this .loggingSystem .availableClasses (ObjectMapper .class .getName ());
206
- assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2.json" , "log4j2.jsn" , "log4j2.xml" );
210
+ assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
211
+ "log4j2-test.json" , "log4j2-test.jsn" , "log4j2-test.xml" , "log4j2.properties" , "log4j2.json" ,
212
+ "log4j2.jsn" , "log4j2.xml" );
207
213
}
208
214
209
215
@ Test
210
216
void configLocationsWithJacksonDataformatYaml () {
211
217
this .loggingSystem .availableClasses ("com.fasterxml.jackson.dataformat.yaml.YAMLParser" );
212
- assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2.yaml" , "log4j2.yml" , "log4j2.xml" );
218
+ assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
219
+ "log4j2-test.yaml" , "log4j2-test.yml" , "log4j2-test.xml" , "log4j2.properties" , "log4j2.yaml" ,
220
+ "log4j2.yml" , "log4j2.xml" );
213
221
}
214
222
215
223
@ Test
216
224
void configLocationsWithJacksonDatabindAndDataformatYaml () {
217
225
this .loggingSystem .availableClasses ("com.fasterxml.jackson.dataformat.yaml.YAMLParser" ,
218
226
ObjectMapper .class .getName ());
219
- assertThat (this .loggingSystem .getStandardConfigLocations ()).contains ("log4j2.yaml" , "log4j2.yml" , "log4j2.json" ,
220
- "log4j2.jsn" , "log4j2.xml" );
227
+ assertThat (this .loggingSystem .getStandardConfigLocations ()).containsExactly ("log4j2-test.properties" ,
228
+ "log4j2-test.yaml" , "log4j2-test.yml" , "log4j2-test.json" , "log4j2-test.jsn" , "log4j2-test.xml" ,
229
+ "log4j2.properties" , "log4j2.yaml" , "log4j2.yml" , "log4j2.json" , "log4j2.jsn" , "log4j2.xml" );
221
230
}
222
231
223
232
@ Test
224
233
void springConfigLocations () {
225
234
String [] locations = getSpringConfigLocations (this .loggingSystem );
226
- assertThat (locations ).containsExactly ("log4j2-spring.properties" , "log4j2-spring.xml" );
235
+ assertThat (locations ).containsExactly ("log4j2-test-spring.properties" , "log4j2-test-spring.xml" ,
236
+ "log4j2-spring.properties" , "log4j2-spring.xml" );
227
237
}
228
238
229
239
@ Test
230
240
void exceptionsIncludeClassPackaging (CapturedOutput output ) {
231
241
this .loggingSystem .beforeInitialize ();
232
- this .loggingSystem .initialize (null , null , getLogFile (null , tmpDir ()));
242
+ this .loggingSystem .initialize (null , getRelativeClasspathLocation ("log4j2-file.xml" ),
243
+ getLogFile (null , tmpDir ()));
233
244
this .logger .warn ("Expected exception" , new RuntimeException ("Expected" ));
234
245
String fileContents = contentOf (new File (tmpDir () + "/spring.log" ));
235
246
assertThat (fileContents ).contains ("[junit-" );
@@ -249,7 +260,8 @@ void customExceptionConversionWord(CapturedOutput output) {
249
260
try {
250
261
this .loggingSystem .beforeInitialize ();
251
262
this .logger .info ("Hidden" );
252
- this .loggingSystem .initialize (null , null , getLogFile (null , tmpDir ()));
263
+ this .loggingSystem .initialize (null , getRelativeClasspathLocation ("log4j2-file.xml" ),
264
+ getLogFile (null , tmpDir ()));
253
265
this .logger .warn ("Expected exception" , new RuntimeException ("Expected" , new RuntimeException ("Cause" )));
254
266
String fileContents = contentOf (new File (tmpDir () + "/spring.log" ));
255
267
assertThat (fileContents ).contains ("java.lang.RuntimeException: Expected" ).doesNotContain ("Wrapped by:" );
@@ -278,6 +290,14 @@ void initializationIsOnlyPerformedOnceUntilCleanedUp() {
278
290
verify (listener , times (4 )).propertyChange (any (PropertyChangeEvent .class ));
279
291
}
280
292
293
+ private String getRelativeClasspathLocation (String fileName ) {
294
+ String defaultPath = ClassUtils .getPackageName (getClass ());
295
+ defaultPath = defaultPath .replace ('.' , '/' );
296
+ defaultPath = defaultPath + "/" + fileName ;
297
+ defaultPath = "classpath:" + defaultPath ;
298
+ return defaultPath ;
299
+ }
300
+
281
301
static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
282
302
283
303
private List <String > availableClasses = new ArrayList <>();
0 commit comments