Generating Logs Output in JSON with Logback
To get logs output in JSON with Logback, we have to use additional libraries such as Logstash or logback JSON support libraries.
In this tutorial, we will show you both ways of generating JSON logs with Logback.
Getting Logs in JSON using Logstash Library with Logback
First, add dependencies to your project:
For Maven
For Gradle
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.3.0-alpha13'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha13'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha7'
implementation group: 'net.logstash.logback', name: 'logstash-logback-encoder', version: '7.1.1'
You can find other version of Logstash here in the Logstash Maven repository.
Now, we need to add configuration like the following in the logback.xml file:
Here is an example code of logging:
import static net.logstash.logback.argument.StructuredArguments.entries;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TransactionServiceImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionServiceImpl.class);
public void inititate(String senderId, String receiverId, double trxnAmount) {
Map<String, Object> logKeyValue = new HashMap<String, Object>();
logKeyValue.put("senderId", senderId);
logKeyValue.put("receiverId", receiverId);
logKeyValue.put("trxnAmount", trxnAmount);
LOGGER.debug("initiating transaction", entries(logKeyValue));
try {
int testResult = (int) trxnAmount / 0;
LOGGER.info("Result = {}", testResult);
} catch (Exception ex) {
LOGGER.error("Exception caught while initiating transaction " + ex);
}
}
}
The logs output will look like the following:
{
"timestamp": "2022-04-19T23:41:52.539-07:00",
"message": "initiating transaction",
"logger_name": "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"level": "DEBUG",
"level_value": 10000,
"senderId": "1",
"receiverId": "2",
"trxnAmount": 1850
}
"timestamp": "2022-04-19T23:41:52.539-07:00",
"message": "initiating transaction",
"logger_name": "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"level": "DEBUG",
"level_value": 10000,
"senderId": "1",
"receiverId": "2",
"trxnAmount": 1850
}
Getting Logs in JSON with Logback Libraries
Add dependencies to your project:
For Maven
For Gradle
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.3.0-alpha13'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha13'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha7'
implementation group: 'ch.qos.logback.contrib', name: 'logback-jackson', version: '0.1.5'
implementation 'ch.qos.logback.contrib:logback-json-classic:0.1.5'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.2.2'
Add configuration like the following in the logback.xml file:
The logs output will look like the following:
{
"timestamp" : "2022-04-20 00:35:35",
"level" : "DEBUG",
"thread" : "main",
"logger" : "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"message" : "initiating transaction",
"context" : "default"
}{
"timestamp" : "2022-04-20 00:35:35",
"level" : "ERROR",
"thread" : "main",
"logger" : "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"message" : "Exception caught while initiating transaction java.lang.ArithmeticException: / by zero",
"context" : "default"
}
"timestamp" : "2022-04-20 00:35:35",
"level" : "DEBUG",
"thread" : "main",
"logger" : "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"message" : "initiating transaction",
"context" : "default"
}{
"timestamp" : "2022-04-20 00:35:35",
"level" : "ERROR",
"thread" : "main",
"logger" : "com.tutorialsbuddy.logging.example.transaction.impl.TransactionServiceImpl",
"message" : "Exception caught while initiating transaction java.lang.ArithmeticException: / by zero",
"context" : "default"
}