For logging we are using Logback. Currently we are using Splunk for viewing the Spring Boot log. As per the project requirement we need to move towards Kibana. For that first we should have log in JSON format so that Kibana can easily process it. Currently we are not replacing Splunk with Kibana but when integration successful with Kibana then gradually we can replace Splunk in all the projects.
Hence as per the requirement we need existing log i.e. in text log but for R&D purpose we need JSON log as well.
This is very simple. You just need to add
logstash-logback-encoder
dependency inpom.xml
file and write some configuration in yourlogback.xml
file. Please follow the steps below to fulfill your requirement.Step1: Add
logstash-logback-encoder
dependency inpom.xml
.Step2: Create
logback.xml
file insidesrc/main/resources/
and copy-paste the below content.Step3: Now you can use log statement anywhere in the class. Let's say you are using in the main class of Spring Boot.
application-text.log
will look like thisapplication-json.log
will look like thisNote: Here @Slf4j is a Lombok annotation. This means without creating LoggerFactory you can directly use log methods. Fo that you need to install Lombok in your IDE & add a dependency in pom.xml
If you need a demo project, please visit https://github.com/altafjava/json-log