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