Contextual Logging in Event-Driven Java Services

Moving away from thread-based context

Randal Kamradt Sr
Javarevisited

--

Log pile
Image by Jeon Sang-O from Pixabay

Microservices tend to do one thing and are dependent on other microservices for help in performing a complete transaction. As the action ping-pongs through the services, it helps to have a trace ID so you can tell who’s making the requests. That way you can follow through the complete transaction even though it may involve several services.

One problem that modern microservices face is that they are event-driven internally as well so using thread-local storage is no longer an option. In addition, using non-blocking I/O also prevents thread-local storage because the flow for a request no longer only goes through a single thread. In this article, I will solve both issues using the event-driven application that I created in the article CQRS in Java Microservices. You may wish to look over that article before you start to familiarize yourself with it.

The application is for used vehicle inventory and allows you to buy a used vehicle, move it from lot to lot, and sell the vehicle. But it doesn’t really demonstrate service to service calls. So I’m going to add in a service that deals with “lots”, allowing you to create a lot so that the service that moves vehicles from lot to lot will be able to look up information about the lot. I won’t bore you with…

--

--