Intermediate 20 min readModule: Module 9: System Logging, journalctl & Log Rotation
System Logging with journalctl & Logrotate
Query system and service logs with journalctl, filter by unit, priority, and timestamps, and manage disk space with logrotate configuration.
What You Will Learn in This Lesson
- Querying binary systemd journal logs using journalctl
- Filtering logs by unit (`-u`), boot session (`-b`), and priority level (`-p err`)
- Viewing real-time live log streams (`journalctl -f`)
- Configuring automated log rotation policies in /etc/logrotate.d/
Introduction & Core Concept
Linux servers generate continuous operational telemetry. On modern Ubuntu and Debian systems, systemd-journald captures kernel messages, system events, daemon output, and standard error streams in a high-speed indexed binary log format. The logrotate daemon automatically compresses, archives, and removes old text logs to protect disk space.
WHY DOES THIS MATTER IN THE REAL WORLD?
When servers crash or services encounter errors, system logs provide the definitive audit trail. Mastering journalctl filtering and logrotate policies ensures you can diagnose root causes rapidly without running out of server disk space.
Syntax & Structure
bash
journalctl -u nginx.service -fjournalctl -p err -bjournalctl --since "1 hour ago"logrotate -d /etc/logrotate.confDiagnosing System Issues with journalctl and Logrotate
bashbash
123456789101112131415161718192021222324252627#!/usr/bin/env bash# Production Log Analysis with journalctlecho "=== 1. System Errors on Current Boot ==="journalctl -b -p err..emerg --no-pager | head -n 20echo -e "=== 2. Inspecting Specific Service Logs (Last 30 Minutes) ==="# journalctl -u myapp.service --since "30 min ago" --no-pagerecho -e "=== 3. Custom Logrotate Configuration Example (/etc/logrotate.d/kwas-app) ==="cat << 'EOF'/var/log/kwas-app/*.log {dailymissingokrotate 14compressdelaycompressnotifemptycreate 0640 appuser appusersharedscriptspostrotatesystemctl reload kwas-app.service > /dev/null 2>&1 || trueendscript}EOF
Line-by-Line Technical Breakdown
1Syslog Priority Levels: 0=Emergency, 1=Alert, 2=Critical, 3=Error, 4=Warning, 5=Notice, 6=Informational, 7=Debug. `journalctl -p 3` returns all entries at Error level or higher.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[BASH]
BASH SOURCE EDITOR
Interactive Live CodeCommon Mistakes & How to Avoid Them
#1: Deleting an active open log file with rm without reloading the logging daemon.
If you rm a file that a running process still has open, Linux maintains the inode open in memory. The disk space is NOT freed until the process is restarted.
Incorrect / Antipattern
rm /var/log/app/access.logCorrect / Professional Solution
truncate -s 0 /var/log/app/access.log
# OR configure proper logrotateIndustry Best Practices & Professional Standards
- Use `journalctl -u <service> -n 100 --no-pager` for rapid diagnostic inspection in scripts.
- Always specify compression (`compress`, `delaycompress`) in logrotate configurations.
- Truncate active log files (`truncate -s 0 file.log`) instead of deleting them directly when performing emergency disk cleanup.
Lesson Summary & Core Takeaways
- journalctl provides indexed, binary log querying across all systemd units.
- Filter logs by unit (`-u`), boot (`-b`), priority (`-p`), and timeframe (`--since`).
- logrotate manages automated rotation, compression, and disk space preservation.