Introduction
In modern application development, especially in microservices and distributed systems, logs are one of the most important sources of information for debugging, monitoring, and performance analysis. When applications run across multiple servers or containers, logs get scattered, making it difficult to trace issues.
Centralized logging solves this problem by collecting logs from different services and storing them in a single place where they can be searched, analyzed, and visualized.
One of the most popular solutions for centralized logging is the ELK Stack, which consists of Elasticsearch, Logstash, and Kibana.
This article provides a detailed, step-by-step guide on how to implement centralized logging using the ELK stack, along with examples, real-world use cases, advantages, and best practices.
What is ELK Stack?
ELK stands for:
Elasticsearch → Search and analytics engine
Logstash → Data processing and pipeline tool
Kibana → Visualization and dashboard tool
These three components work together to collect, process, store, and visualize logs.
How ELK Stack Works
Flow of Data
Applications generate logs
Logs are collected using agents (e.g., Filebeat)
Logstash processes and transforms logs
Elasticsearch stores and indexes logs
Kibana is used to search and visualize logs
This pipeline enables real-time log monitoring and analysis.
Step 1: Install Elasticsearch
Elasticsearch is responsible for storing and indexing logs.
sudo apt update
sudo apt install elasticsearch
Start and enable the service:
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
Explanation
Elasticsearch runs as a service
It stores logs in a searchable format
It provides fast querying capabilities
Step 2: Install Logstash
Logstash collects and processes logs before sending them to Elasticsearch.
sudo apt install logstash
Create a Logstash Configuration
input {
beats {
port => 5044
}
}
filter {
json {
source => "message"
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "app-logs-%{+YYYY.MM.dd}"
}
}
Explanation
Input listens for logs from Beats (like Filebeat)
Filter parses logs into structured JSON
Output sends logs to Elasticsearch
Join the conversation! Your thoughts help the community grow.