Image by Robert Balog from Pixabay

API Log tracking

Deepak Nair
2 min readNov 21, 2020

--

I have a set of web services which communicates with each other and I want to build an easy way for tracking logs across these services.

For example I have services A, B and C and consider the communication is from A -> B -> C. I am trying to build a solution for below requirements

Requirements

  1. Track current context for A, B and C using a log id.
  2. Track request between two immediate services A -> B and B -> C.
  3. Track request across multiple services A -> B -> C using a single log id.
  4. No explicit logging for log context ids.

Implementation details

Track current context for A, B and C using a log id.

A unique id will be created and added to log context when service api is called.

Track request between two immediate services A -> B and B -> C.

When a service sends request to other service, the sender will send its contextId in request header “Message-ID”. The receiver service will extract this value from header and add to its log context.

Track request across multiple services A -> B -> C using a single log id.

When a service receives an incoming request, it will check any traceId is present by checking request header “Trace-ID”, if it is not present it will generate one and add to the log context. It will pass this generated or received trace id in all outgoing communication to other services. The other services are also expected to do the same with traceId.

No explicit logging for log context ids.

All log tracking ids contextId, requestId and traceId should come in all application logs without any explicit logging.

To achieve this I have decided to use Mapped Diagnostic Context (MDC) , logback configuration and a servlet filter. All required context ids will be added to MDC by servlet filter.

Servlet filter

Logback configuration

Logging in application class

Sample log in console

Github link

--

--