Skip to content

Commit 7f53814

Browse files
authored
Merge pull request eugenp#12130 from hkhan/JAVA-11417-fix-log4j2-test-failure
[JAVA-11417] Fix log4j2 JSON integration test
2 parents 09095ca + dced18c commit 7f53814

1 file changed

Lines changed: 37 additions & 26 deletions

File tree

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
11
package com.baeldung.logging.log4j2.tests;
22

3-
import static org.junit.Assert.assertTrue;
4-
5-
import java.io.ByteArrayOutputStream;
6-
import java.io.IOException;
7-
import java.io.PrintStream;
8-
3+
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
96
import org.apache.logging.log4j.LogManager;
107
import org.apache.logging.log4j.Logger;
8+
import org.apache.logging.log4j.core.Appender;
9+
import org.apache.logging.log4j.core.appender.WriterAppender;
10+
import org.apache.logging.log4j.core.layout.JsonLayout;
11+
import org.junit.After;
1112
import org.junit.Before;
1213
import org.junit.Test;
1314

14-
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
15-
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import java.io.CharArrayWriter;
16+
import java.io.Writer;
17+
18+
import static org.junit.Assert.assertTrue;
1619

1720
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
1821

19-
private static Logger logger;
20-
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
21-
private PrintStream ps = new PrintStream(consoleOutput);
22+
private Appender appender;
23+
private Logger logger;
24+
private final Writer writer = new CharArrayWriter();
2225

2326
@Before
2427
public void setUp() {
25-
// Redirect console output to our stream
26-
System.setOut(ps);
2728
logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
29+
30+
appender = WriterAppender.newBuilder()
31+
.setTarget(writer)
32+
.setLayout(JsonLayout.newBuilder().build())
33+
.setName("json_layout_for_testing")
34+
.build();
35+
appender.start();
36+
37+
((org.apache.logging.log4j.core.Logger) logger).addAppender(appender);
2838
}
2939

3040
@Test
31-
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
41+
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() throws Exception {
3242
logger.debug("Debug message");
33-
String currentLog = consoleOutput.toString();
34-
assertTrue(currentLog.isEmpty());
35-
assertTrue(isValidJSON(currentLog));
43+
44+
writer.flush();
45+
assertTrue(isValidJSON(writer.toString()));
3646
}
3747

38-
public static boolean isValidJSON(String jsonInString) {
39-
try {
40-
final ObjectMapper mapper = new ObjectMapper();
41-
mapper.readTree(jsonInString);
42-
return true;
43-
} catch (IOException e) {
44-
return false;
45-
}
48+
@After
49+
public void cleanup() {
50+
((org.apache.logging.log4j.core.Logger) logger).removeAppender(appender);
4651
}
47-
}
52+
53+
private static boolean isValidJSON(String jsonInString) throws Exception {
54+
JsonNode jsonNode = new ObjectMapper().readTree(jsonInString);
55+
return jsonNode.get("message").asText().equals("Debug message");
56+
}
57+
58+
}

0 commit comments

Comments
 (0)