One of the biggest difficulties when working with Microservices (or with other Distributed systems)
is to debug if any problems occur. It is because the business logic is divided into several small places.
The code bug in one service can result in a cascading series of issues in many related services.
Tracing which service is the root cause of the issue is always a challenging mission. By
implementing a good Logging solution, you can reduce the time it takes to discover the bug. It also
helps you feel more confident about what happened in your code as well as makes the problem easier
to reason about.
Let’s get your feet wet!
So you decided it’s time to build a logging solution for your Microservices system, here are some
steps that you probably need to do in order to build that.
- First, design and implement your logging module so that it works well in one microservice.
- Apply it to all the services in the system.
- Implement a method to link all the correlated logs in different services.
- Set up centralised logging server for processing and querying the log data.
- Define which data you need to put into the log entries for better investigation.
Design your Logging library
Before starting with a full Logging solution for the whole large application, it is important that
you get your smallest building block to work properly. You will first need to build a logging
solution that can work well in one service, and then apply to all other services. You have to define
a logging standard that all the other services will follow so that you can store all the
log entries into another logging backend storage for later investigation.
The simplest logging way is to write the log immediately whenever you want. For example, when you
receive one API request, when the HTTP request is done processing or when the server finishes
update one record in the database. However, you will soon end up with a bunch of messy log entries
because the web server usually processes multiple requests at the same time and you don’t know which
ones have the correlation with the others. This is quite common in the concurrent and parallel world
where the system can handle different tasks at once. You need to design a logging backend that can
associate all the related log entries into one.