Logging Terpusat
Mengelola log dari banyak sumber di satu tempat
Masalah Log Tersebar
- Aplikasi berjalan di banyak server/container
- Sulit tail log di masing-masing node
- Korelasi log antar service memakan waktu
- Log hilang saat container/pod restart
Solusi: Centralized Logging
- Mengumpulkan log dari semua sumber ke satu tempat
- Query dan analisis dari satu interface
- Retention dan compliance lebih mudah
Arsitektur Centralized Logging
Log Shipper
- Mengirim log dari source ke collector
- Contoh: Fluentd, Fluent Bit, Filebeat, Logstash
Log Collector / Storage
- Menerima, mengindeks, dan menyimpan log
- Contoh: Elasticsearch, Loki, ClickHouse
Query Interface
- UI untuk mencari dan visualisasi log
- Contoh: Kibana, Grafana, Datadog
ELK Stack
Komponen
- Elasticsearch: Search engine dan storage log
- Logstash: Processing pipeline (parse, filter, enrich)
- Kibana: Visualisasi dan query log
Flow Data
Application → Logstash → Elasticsearch → KibanaKeunggulan ELK
- Full-text search yang powerful
- Query complex dengan Lucene syntax
- Ekosystem mature
Kekurangan ELK
- Resource intensive (RAM dan CPU tinggi)
- Operational complexity tinggi
- Biaya storage besar untuk log volume tinggi
Grafana Loki
Konsep
- Logging system terinspirasi Prometheus
- Label-based indexing (bukan full-text)
- Lebih ringan dan murah dibanding ELK
- Terintegrasi native dengan Grafana
Arsitektur
Application → Promtail → Loki → GrafanaKonfigurasi Promtail
server:
http_listen_port: 9080
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: app-logs
static_configs:
- targets:
- localhost
labels:
job: app
__path__: /var/log/app/*.logLogQL Query
# Cari log dengan kata "error"
{job="app"} |= "error"
# Cari log dengan level error
{job="app"} | json | level="error"
# Metric dari log (jumlah error per menit)
sum(rate({job="app"} |= "error" [1m]))Structured Logging
Format JSON
{
"timestamp": "2024-01-15T10:30:00Z",
"level": "error",
"message": "Database connection failed",
"service": "api",
"trace_id": "abc123",
"duration_ms": 150
}Keuntungan Structured Logging
- Mudah parse dan query
- Korelasi antar log lebih mudah
- Dapat diubah menjadi metric
- Compatible dengan semua log aggregator
Best Practice Logging
Level Log
- DEBUG: Informasi detail untuk development
- INFO: Kejadian normal aplikasi
- WARN: Kejadian abnormal tapi tidak fatal
- ERROR: Kejadian yang memerlukan perhatian
- FATAL: Error kritis, aplikasi berhenti
Field Penting
- Timestamp (ISO 8601)
- Service name
- Trace/Correlation ID
- User ID (jika relevan)
- Environment
Hal yang Dihindari
- Log password atau secret
- Log data PII tanpa masking
- Log terlalu verbose di production
- Log dalam loop dengan high frequency
Cloud Logging Services
- AWS CloudWatch Logs: Native AWS
- Google Cloud Logging: Native GCP
- Azure Monitor Logs: Native Azure
- Datadog Log Management: SaaS
- Splunk: Enterprise logging