Merge pull request #14930 from hmdrzsharifi/BAEL-6912
Bael 6912: Support JsonEncoder in Logback
This commit is contained in:
commit
b7696979c2
@ -16,6 +16,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
public class JSONLayoutIntegrationTest {
|
public class JSONLayoutIntegrationTest {
|
||||||
|
|
||||||
private static Logger logger;
|
private static Logger logger;
|
||||||
|
private static Logger jsonlogger;
|
||||||
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
||||||
private PrintStream ps = new PrintStream(consoleOutput);
|
private PrintStream ps = new PrintStream(consoleOutput);
|
||||||
|
|
||||||
@ -23,16 +24,24 @@ public class JSONLayoutIntegrationTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
// Redirect console output to our stream
|
// Redirect console output to our stream
|
||||||
System.setOut(ps);
|
System.setOut(ps);
|
||||||
logger = LoggerFactory.getLogger("jsonLogger");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
|
public void givenJsonLayout_whenLogInJSON_thenOutputIsCorrectJSON() {
|
||||||
|
logger = LoggerFactory.getLogger("jsonLogger");
|
||||||
logger.debug("Debug message");
|
logger.debug("Debug message");
|
||||||
String currentLog = consoleOutput.toString();
|
String currentLog = consoleOutput.toString();
|
||||||
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonEncoder_whenLogInJSON_thenOutputIsCorrectJSON() {
|
||||||
|
jsonlogger = LoggerFactory.getLogger("jsonEncoderLogger");
|
||||||
|
jsonlogger.debug("Debug message");
|
||||||
|
String currentLog = consoleOutput.toString();
|
||||||
|
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isValidJSON(String jsonInString) {
|
public static boolean isValidJSON(String jsonInString) {
|
||||||
try {
|
try {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
@ -19,10 +19,18 @@
|
|||||||
</layout>
|
</layout>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
|
<appender name="jsonEncoder" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder class="ch.qos.logback.classic.encoder.JsonEncoder"/>
|
||||||
|
</appender>
|
||||||
|
|
||||||
<logger name="jsonLogger" level="TRACE">
|
<logger name="jsonLogger" level="TRACE">
|
||||||
<appender-ref ref="json" />
|
<appender-ref ref="json" />
|
||||||
</logger>
|
</logger>
|
||||||
|
|
||||||
|
<logger name="jsonEncoderLogger" level="TRACE">
|
||||||
|
<appender-ref ref="jsonEncoder" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
<root level="debug">
|
<root level="debug">
|
||||||
<appender-ref ref="map" />
|
<appender-ref ref="map" />
|
||||||
<appender-ref ref="badMap" />
|
<appender-ref ref="badMap" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user