Log Management
Cheat sheet lengkap untuk mengelola, memonitor, dan menganalisis log sistem di Linux.
Lokasi Log di Linux
Semua log sistem berada di /var/log/:
/var/log/
├── syslog # Log sistem utama (Ubuntu/Debian)
├── messages # Log sistem utama (RHEL/CentOS)
├── auth.log # Autentikasi login (Ubuntu/Debian)
├── secure # Autentikasi login (RHEL/CentOS)
├── kern.log # Kernel messages
├── dmesg # Kernel ring buffer (boot)
├── dpkg.log # Package manager (apt)
├── boot.log # Boot messages
├── faillog # Failed login attempts
├── lastlog # Last login each user
├── nginx/ # Nginx access & error logs
│ ├── access.log
│ └── error.log
├── mysql/ # MySQL/MariaDB logs
│ └── error.log
├── apache2/ # Apache logs
│ ├── access.log
│ └── error.log
└── journal/ # Journalctl persistent storage
journalctl - Log Systemd Modern
journalctl adalah alat utama untuk membaca log dari systemd journal.
Dasar
# Semua log (dari boot terakhir)
journalctl
# Log sejak boot ini
journalctl -b
# Log dari boot sebelumnya
journalctl -b -1
journalctl -b -2
# Follow mode (real-time, seperti tail -f)
journalctl -f
Filter by Service
# Log service tertentu
journalctl -u nginx
journalctl -u nginx -u sshd
journalctl -u docker.service
# Log unit spesifik
journalctl -u nginx -b # service nginx sejak boot ini
journalctl -u sshd --since today # service sshd hari ini
Filter by Priority / Severity
# Level priority:
# 0 = emerg, 1 = alert, 2 = crit, 3 = err
# 4 = warning, 5 = notice, 6 = info, 7 = debug
# Hanya error ke atas
journalctl -p err
# Warning ke atas
journalctl -p warning
# Gabung dengan service
journalctl -u nginx -p err
Filter by Time
# Sejak waktu tertentu
journalctl --since "2026-06-20"
journalctl --since "2026-06-20 10:00:00"
# Sampai waktu tertentu
journalctl --until "2026-06-21"
# Relative time
journalctl --since "2 hours ago"
journalctl --since yesterday
journalctl --since "1 day ago" --until "30 minutes ago"
Output Format
# Format JSON (untuk parsing)
journalctl -o json
journalctl -o json-pretty
# Format verbose (metadata lengkap)
journalctl -o verbose
# Hanya pesan (tanpa metadata)
journalctl -o cat
# Short (default)
journalctl -o short-full
Contoh Praktis journalctl
# Cek error nginx real-time
journalctl -u nginx -f -p err
# Cek semua error dari semalam
journalctl -u sshd --since "last night" -p err
# Cek log boot terakhir
journalctl -b -p warning
# Cek siapa yang login hari ini
journalctl -u sshd --since today | grep "Accepted"
# Cek failed login
journalctl -u sshd --since today | grep "Failed password"
# Cek disk usage journal
journalctl --disk-usage
# Bersihkan journal (simpan 500MB terakhir)
sudo journalctl --vacuum-size=500M
# Bersihkan journal (simpan 2 minggu)
sudo journalctl --vacuum-time=2weeks
Logrotate - Rotasi & Kompresi Log
Logrotate mencegah log memenuhi disk dengan merotasi, mengompresi, dan menghapus log lama.
Konfigurasi Dasar
File konfigurasi: /etc/logrotate.conf (global) dan
/etc/logrotate.d/ (per-service).
/var/log/nginx/*.log {
daily # Rotasi setiap hari
missingok # Tidak error jika file hilang
rotate 14 # Simpan 14 rotasi terakhir
compress # Kompres dengan gzip
delaycompress # Tunda kompresi 1 rotasi
notifempty # Jangan rotasi jika kosong
create 0640 www-data adm # Buat file baru dengan permission
sharedscripts # Script dijalankan sekali untuk semua log
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
Konfigurasi Custom
/var/log/myapp/*.log {
weekly
rotate 4
compress
maxage 30 # Hapus log lebih dari 30 hari
size 100M # Rotasi jika mencapai 100MB
missingok
notifempty
create 0640 myapp myapp
}
Test & Manual Run
# Dry-run (tidak benar-benar rotate)
sudo logrotate -d /etc/logrotate.conf
# Force rotate
sudo logrotate -f /etc/logrotate.d/nginx
# Rotate dengan verbose
sudo logrotate -v /etc/logrotate.d/nginx
Parameter Penting Logrotate
| Parameter | Fungsi |
|---|---|
daily |
Rotasi setiap hari |
weekly |
Rotasi setiap minggu |
monthly |
Rotasi setiap bulan |
size 100M |
Rotasi jika ukuran mencapai 100MB |
rotate 14 |
Simpan 14 log lama |
compress |
Kompres file lama dengan gzip |
delaycompress |
Jangan kompres file kemarin (masih dibaca) |
missingok |
Jangan error jika file tidak ada |
notifempty |
Jangan rotasi jika file kosong |
maxage 30 |
Hapus log lebih dari 30 hari |
postrotate |
Script setelah rotasi |
prerotate |
Script sebelum rotasi |
sharedscripts |
Script sekali untuk semua file (bukan per file) |
️ Analisis Log dengan grep
Gunakan grep untuk mencari pola dalam log.
Pola Dasar
# Cari error
grep -i "error" /var/log/syslog
# Cari IP tertentu
grep "192.168.1.100" /var/log/nginx/access.log
# Cari dengan regex
grep -E "(error|failed|critical)" /var/log/syslog
Options Penting
-i # Case-insensitive
-v # Invert match (kecualikan)
-n # Tampilkan nomor baris
-c # Hitung jumlah match
-A 3 # Tampilkan 3 baris SETELAH match
-B 3 # Tampilkan 3 baris SEBELUM match
-C 3 # Tampilkan 3 baris SEBELUM & SETELAH
-w # Match whole word
-o # Hanya tampilkan bagian yang match
Contoh Praktis grep untuk Log
# Cari error di syslog dengan konteks
grep -i -C 3 "error" /var/log/syslog | less
# Hitung jumlah error per jam
grep "error" /var/log/syslog | wc -l
# Cari failed SSH login
grep "Failed password" /var/log/auth.log
# Cari successful SSH login
grep "Accepted" /var/log/auth.log
# Cari IP unik yang gagal login
grep "Failed password" /var/log/auth.log | grep -oP "from \K[0-9.]+" | sort -u
# Cari pattern dalam log terkompresi (.gz)
zgrep "error" /var/log/syslog.1.gz
️ Real-time Monitoring
tail -f
# Follow log file
tail -f /var/log/nginx/access.log
# Follow dengan highlight (menggunakan multitail atau ccze)
tail -f /var/log/syslog | ccze -A
# Follow multiple files
tail -f /var/log/nginx/access.log /var/log/nginx/error.log
journalctl -f
# Follow semua log
journalctl -f
# Follow service tertentu
journalctl -u nginx -f
# Follow dengan filter priority
journalctl -u sshd -f -p err
multitail (Install jika perlu)
sudo apt install multitail
# Monitor multiple files
multitail -s 2 /var/log/syslog /var/log/auth.log
# Monitor dengan judul
multitail -l "journalctl -u nginx -f" -l "tail -f /var/log/nginx/access.log"
lnav (Log Navigator - Advanced)
sudo apt install lnav
# Buka log viewer interaktif
lnav /var/log/
lnav /var/log/syslog /var/log/auth.log
lnav fitur: syntax highlight, timeline, filtering, search, histogram.
AUDITD - Audit Sistem
auditd memonitor aktivitas sistem secara detail (file changes, syscalls, login).
Install & Start
sudo apt install auditd
sudo systemctl enable --now auditd
Aturan Dasar auditctl
# Monitor /etc/passwd untuk perubahan
sudo auditctl -w /etc/passwd -p wa -k passwd_changes
# Monitor /etc/shadow
sudo auditctl -w /etc/shadow -p wa -k shadow_changes
# Monitor login attempts
sudo auditctl -w /var/log/faillog -p rwa -k logins
# Lihat aturan aktif
sudo auditctl -l
Cari Log Audit (ausearch)
# Cari berdasarkan key
sudo ausearch -k passwd_changes
# Cari berdasarkan user
sudo ausearch -ua labkom
# Cari berdasarkan tipe
sudo ausearch -m LOGIN
# Cari dalam rentang waktu
sudo ausearch -ts today -te now
Laporan (aureport)
# Laporan login
sudo aureport -l
# Laporan perubahan file
sudo aureport -f
# Laporan authentication
sudo aureport -au
# Ringkasan
sudo aureport -x
Custom Log dengan Rsyslog
rsyslog adalah daemon logging yang bisa dikonfigurasi untuk routing log.
Konfigurasi Dasar
# Template untuk log custom
template(name="MyFormat" type="string"
string="%TIMESTAMP% %HOSTNAME% %syslogtag%%msg%\n")
# Kirim log aplikasi ke file terpisah
if $programname == 'myapp' then /var/log/myapp.log
& stop
# Kirim log nginx ke file spesifik
if $programname == 'nginx' then /var/log/nginx/system.log
& stop
Forward Log ke Server Pusat
# Kirim semua log ke logserver:514 (UDP)
*.* @192.168.1.200:514
# Kirim via TCP (lebih reliable)
*.* @@logserver.example.com:514
sudo systemctl restart rsyslog
Analisis Nginx Log dengan GoAccess
GoAccess adalah analyzer log real-time berbasis terminal.
Install
sudo apt install goaccess
Penggunaan Dasar
# Analisis real-time dari terminal
goaccess /var/log/nginx/access.log -c
# Generate HTML report
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED
# Real-time dashboard
goaccess /var/log/nginx/access.log --log-format=COMBINED
Dashboard GoAccess Menampilkan:
- Unique visitors
- Requested files
- Status codes (404, 500, etc)
- OS & Browser
- Top pages & referrers
- Geo location (jika IP database terinstall)
- Real-time data
Troubleshooting
Disk Full karena Log
# Cek penggunaan disk
df -h
# Cek ukuran folder log
du -sh /var/log/
du -sh /var/log/* | sort -rh | head -10
# Bersihkan journal
sudo journalctl --vacuum-size=200M
# Hapus log lama secara manual
sudo find /var/log -name "*.gz" -mtime +30 -delete
sudo find /var/log -name "*.log.*" -mtime +30 -delete
Permission Denied pada Log
# Cek permission
ls -la /var/log/nginx/
# Fix permission
sudo chmod 640 /var/log/nginx/error.log
sudo chown -R www-data:adm /var/log/nginx/
Service Tidak Menulis Log
# Cek apakah service berjalan
systemctl status nginx
# Cek journal untuk service
journalctl -u nginx -n 20
# Cek konfigurasi logging service
# Nginx: /etc/nginx/nginx.conf - cek access_log & error_log direktif
# MySQL: /etc/mysql/my.cnf - cek log-error
# Restart service
sudo systemctl restart nginx
Logrotate Tidak Berjalan
# Cek status logrotate timer
systemctl status logrotate.timer
# Manual run dengan debug
sudo logrotate -v -f /etc/logrotate.conf
# Cek konfigurasi error
sudo logrotate -d /etc/logrotate.d/nginx
Best Practices
1. Retensi Log
| Environment | Retention | Contoh Konfigurasi |
|---|---|---|
| Development | 7 hari | rotate 7 |
| Staging | 30 hari | rotate 30 |
| Production | 90 hari | rotate 90 |
| Compliance | 1+ tahun | rotate 365 + backup |
2. Kompresi Log
Selalu aktifkan kompresi untuk log lama. Log rotate biasanya kompres
otomatis ke .gz.
3. Centralized Logging
Untuk multi-server, kirim log ke server pusat:
- Rsyslog - UDP/TCP forwarding
- Logstash - Elastic stack (ELK)
- Grafana Loki - lightweight centralized logging
- Papertrail / Datadog - SaaS solution
4. Monitoring Alert
# Script sederhana monitor error dalam 5 menit
#!/bin/bash
ERRORS=$(journalctl -u nginx --since "5 min ago" -p err | wc -l)
if [ $ERRORS -gt 10 ]; then
fi
5. Log Format Terstruktur
Gunakan format JSON untuk log aplikasi agar mudah diparsing:
{"timestamp":"2026-06-22T10:00:00Z","level":"ERROR","service":"api","message":"DB connection failed","error":"connection refused"}
Ringkasan Perintah
journalctl
| Perintah | Fungsi |
|---|---|
journalctl -u nginx |
Log service nginx |
journalctl -u nginx -f |
Follow log nginx |
journalctl -p err |
Hanya error |
journalctl --since today |
Log hari ini |
journalctl -b |
Log sejak boot terakhir |
journalctl -o json-pretty |
Output JSON |
journalctl --disk-usage |
Cek ukuran journal |
sudo journalctl --vacuum-size=500M |
Bersihkan journal (max 500MB) |
Logrotate
| Perintah | Fungsi |
|---|---|
sudo logrotate -d /etc/logrotate.conf |
Dry-run |
sudo logrotate -f /etc/logrotate.conf |
Force rotate |
sudo logrotate -v /etc/logrotate.d/nginx
|
Rotate dengan verbose |
grep / Analisis
| Perintah | Fungsi |
|---|---|
grep -i "error" /var/log/syslog
|
Cari error case-insensitive |
grep -C 3 "timeout" /var/log/syslog
|
Cari dengan konteks |
zgrep "error" /var/log/syslog.1.gz
|
Cari di log terkompresi |
tail -f /var/log/syslog |
Follow real-time |
Audit
| Perintah | Fungsi |
|---|---|
sudo auditctl -l |
Lihat aturan audit |
sudo ausearch -k passwd |
Cari log audit by key |
sudo aureport -l |
Laporan login |
💡 Golden Rules Log Management:
- Logrotate wajib - mencegah disk full
- journalctl adalah primary tool untuk systemd log
- Gunakan -C (context) saat grep untuk melihat konteks error
- Set retensi log sesuai kebutuhan (7-90 hari)
- Monitor disk usage
/var/log/secara rutin- Untuk production, gunakan centralized logging