Centralized Log Analysis

Centralized log analysis is a way to collect and study log messages from many microservices in one place. It solves some problems that happen when each microservice writes logs only on its own server.

The Problem

Usually, an application writes log messages to files on the same server where it runs. In a system with many microservices, each running on different servers, this can cause problems:

For example, imagine an online shop with these microservices:

Each service writes logs on its own server. Finding a problem would be like searching for a needle in many haystacks.

Here is a simple diagram showing how services connect and where logs are stored:

img5

The Solution

The solution is to add a central log manager. This component collects all logs in one place and helps you analyze them. A good log manager can:

For example, if a payment service fails, the central log manager can quickly show all error messages from this service, making it easier to find the problem.

img6

Solution Requirements

To make centralized logging work well, some rules are needed:

  1. Stream logs to standard output: Each microservice should send logs to stdout instead of writing them to separate files. This makes it easier for the log manager to find logs.
  2. Use correlation IDs: Tag logs with a unique ID for each request. This helps track one user request across many services.
  3. Use a common log format: All log messages should follow the same format. This makes it possible to store them in a database and search easily.

For example, a canonical log format could include:

With these rules, the central log manager can show all logs for one request, even if it touched multiple services.