JavaDream spring boot Distributed Logging in spring boot microservice

Distributed Logging in spring boot microservice

Hi all in this blog we ‘ll learn about distributed logging in spring boot microservice architecture. We all are opting microservice now and many big project are build on microservice pattern. In microservice each microservice has it’s own stuff like it’s own database, configs and logging.

And mostly we call our microservices using some http client like RestTemplate or WebClient. So if we have to trace a call from source to destination means say i have three microservice (Order, Inventory, Payment). Request come to order service then we call inventory service if inventory available we call payment service. What if anything goes wrong during this process.

It ‘ll become hard to debug like in which microservice we have the issue for that particular http call. So just think what if we have a unique identifier for each http call and we just need to pass that identifier in all the services and log that identifier in each microservice. So if anything goes wrong we can search that identifier in all microservice logs and find where the issue is.

We can do it manually. We can create a unique string when call come to Order microservice and pass this identifier in headers while calling other microservice. And each microservice print this identifier when call come to them.

So instead of doing it manually is there any way spring framework do it for us. Yes spring provide this feature and help us to ease out the distributed logs. Previously we use Sleuth but that is deprecated now. Now we do distributed logging using micrometer.

Setup Distributed Logging in spring application

It’s very easy to enable distributed logging in spring application. We just have to follow the below steps.

1- Add below dependency in your project pom file.

<!-- Actuator Dependency (MANDATORY for tracing) -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 <!-- Tracing Dependency-->
 <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>

<!-- Exporter (optional for now, but fine) -->
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>Code language: HTML, XML (xml)

That’s it. We just have to add the dependency. Actuator dependency is must for enabling the tracing. If you do not include this actuator dependency you ‘ll not see any traceId and spanId. So make sure that you have this actuator dependency.

Now to verify that traceId and spanId is there. Just add below logging pattern in you applications.properties file.

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} [%thread] traceId=%X{traceId:-} spanId=%X{spanId:-} - %msg%n

Now run your application. On startup logs you ‘ll see this trace and spanId is empty. Because traceId and spanId only generated when any http call happens. So when you start your application you ‘ll see something like below. This logs coming as per define logging pattern. So on startup these id’s are blank but if you call any controller endpoint it ‘ll generate it automatically.

We have created one GET endpoint let’s try to call that and check the logs. GET endpoint is simple we just make a log statement and return a string as response.

package com.javadream.loggingexample;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

@RestController
@Slf4j
@RequiredArgsConstructor
public class LoggingController {

    private final WebClient webClient;

    @GetMapping("/test")
    public String test(){
        log.info("Calling test API");
        return "Nice Try";
    }

}Code language: CSS (css)

Now we ‘ll run our project and call this endpoint and check our logs after this http call.

See the logs in your console.

You can see the tracId and spanId in logs. traceId remains same across all the microservice logs. only spanId change per microservice.

Now what you can do is create another microservice and do this configuration there. Now call that microservice endpoint using webclient you ‘ll see the same traceId in both the microservice logs.

So this is the way to identify a call from source to destination. Just search for the traceId in each microservice and you ‘ll know which microservice causing the issue.

Hope you like this blog. If you like this please share this with others and help us to grow. Please share your feedback in our comment section.

If you need blog on any specific topic please mention that in our comment section we ‘ll try to cover that in our upcoming blog.

Thanks

Others blog you may like

Deploy Soring boot application on AWS EC2 using docker and PostgresDB connectivity.

How to deploy spring boot application on Kubernetes.

@MappedSuperClass annotation complete guide

Share with others

Related Post