BAEL-1230: Get Java Logs into the JSON Format (#3381)
* Added code for BAEL-1230: Getting log in JSON format * Added code for BAEL-1230: Getting log in JSON format * Improved tests to check correct JSON format in logs * Renamed Test classes to make clear what they do * Fixed wrong indentation
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
a3585ddc8e
commit
740073b6ad
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.logback;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JSONLayoutTest {
|
||||
|
||||
private static Logger logger;
|
||||
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
||||
private PrintStream ps = new PrintStream(consoleOutput);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Redirect console output to our stream
|
||||
System.setOut(ps);
|
||||
logger = LoggerFactory.getLogger("jsonLogger");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
|
||||
logger.debug("Debug message");
|
||||
String currentLog = consoleOutput.toString();
|
||||
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
|
||||
}
|
||||
|
||||
public static boolean isValidJSON(String jsonInString) {
|
||||
try {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.readTree(jsonInString);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,31 @@
|
||||
<configuration debug="true">
|
||||
<configuration debug="false">
|
||||
|
||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||
|
||||
<appender name="map" class="com.baeldung.logback.MapAppender">
|
||||
<prefix>test</prefix>
|
||||
</appender>
|
||||
|
||||
<appender name="badMap" class="com.baeldung.logback.MapAppender"/>
|
||||
<appender name="badMap" class="com.baeldung.logback.MapAppender" />
|
||||
|
||||
# JSON appender
|
||||
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
|
||||
<jsonFormatter
|
||||
class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
|
||||
<prettyPrint>true</prettyPrint>
|
||||
</jsonFormatter>
|
||||
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="jsonLogger" level="TRACE">
|
||||
<appender-ref ref="json" />
|
||||
</logger>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="map"/>
|
||||
<appender-ref ref="badMap"/>
|
||||
<appender-ref ref="map" />
|
||||
<appender-ref ref="badMap" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user