Logs and journalctl: Read and understand logs to debug like a ninja
Understanding Linux Logging Architecture
Linux systems generate extensive logs that are crucial for system monitoring, troubleshooting, and security analysis. Modern distributions use systemd’s journal alongside traditional syslog for comprehensive logging.
Key Logging Components
systemd-journald: The primary logging daemon that collects logs from kernel, system services, and applications. It stores logs in a binary format and provides powerful querying capabilities.
rsyslog/syslog: Traditional text-based logging system that works alongside journald, often storing logs in /var/log/
as human-readable files.
Log Levels: Both systems use standard severity levels (emergency, alert, critical, error, warning, notice, info, debug) to categorize message importance.
Essential journalctl Commands
Basic Log Viewing
View all journal entries:
|
|
Show recent logs (last 10 lines):
|
|
Follow logs in real-time (like tail -f):
|
|
Time-Based Filtering
Show logs from today:
|
|
Show logs from the last hour:
|
|
Show logs between specific times:
|
|
Show logs from last boot:
|
|
List all boots and show logs from specific boot:
Service and Unit Filtering
Show logs for specific service:
Show logs for multiple services:
|
|
Follow specific service logs:
|
|
Priority and Message Filtering
Show only error and critical messages:
|
|
Show warning level and above:
|
|
Search for specific text:
Advanced Filtering Options
Show logs from specific user:
|
|
Show kernel messages:
|
|
Show logs from specific process:
|
|
Show logs with specific field:
|
|
Traditional Log Files Location
Understanding /var/log/
directory structure is essential for comprehensive log analysis:
Critical System Logs
/var/log/syslog
: General system activity log containing kernel messages, system services, and application logs.
/var/log/auth.log
: Authentication attempts, sudo usage, and security-related events. Critical for security monitoring.
/var/log/kern.log
: Kernel messages including hardware detection, driver issues, and system-level errors.
/var/log/dmesg
: Boot-time kernel messages, hardware initialization, and early system startup information.
Service-Specific Logs
/var/log/apache2/
or /var/log/httpd/
: Web server access and error logs.
/var/log/nginx/
: Nginx web server logs.
/var/log/mysql/
: Database server logs including slow queries and errors.
/var/log/mail.log
: Email server logs for postfix, sendmail, or other mail services.
Log Analysis Techniques
Using Traditional Tools
Tail recent entries:
Search with grep:
Analyze patterns with awk:
|
|
Count occurrences:
|
|
Advanced journalctl Analysis
Show unique field values:
Export logs in different formats:
Show log statistics:
Debugging Common Issues
System Boot Problems
Check boot process:
Analyze specific boot:
Service Failures
Investigate failed service:
Check service dependencies:
Performance Issues
Monitor resource usage patterns:
Check disk space issues:
Security Investigations
Authentication failures:
Successful logins:
Sudo usage:
Log Management Best Practices
Journal Configuration
Configure journal retention in /etc/systemd/journald.conf
:
Log Rotation
Traditional logs rotate via logrotate configuration in /etc/logrotate.conf
and /etc/logrotate.d/
.
Manually rotate logs:
|
|
Persistent Journals
Make journal persistent across reboots:
Pro Tips for Log Ninja Skills
Combine multiple sources: Cross-reference journalctl output with traditional log files for complete picture.
Use time correlation: When investigating issues, check logs from all relevant services during the same time window.
Create custom filters: Save complex journalctl commands as aliases or scripts for repeated use.
Monitor in real-time: Use journalctl -f
with specific filters during troubleshooting sessions.
Leverage structured data: Use journalctl’s field filtering capabilities to quickly isolate relevant information.
Regular log review: Establish routine log monitoring to catch issues before they become critical.
Modern Observability: Beyond Traditional Logging
The rise of containers, microservices, and cloud-native applications has transformed how we approach logging and monitoring. Traditional log analysis now forms the foundation layer in a comprehensive observability stack.
The Modern Observability Stack
Metrics Collection: Prometheus scrapes metrics from applications and infrastructure, providing time-series data for performance monitoring.
Log Aggregation: Tools like ELK Stack (Elasticsearch, Logstash, Kibana), Loki, or Fluentd collect and centralize logs from multiple sources.
Visualization: Grafana creates dashboards combining metrics, logs, and traces for unified observability.
Distributed Tracing: Jaeger or Zipkin track requests across microservices to identify bottlenecks.
Container and Kubernetes Logging
Container logs require different approaches since containers are ephemeral:
View Docker container logs:
Kubernetes pod logs:
Integration Strategies
Foundation Layer: Traditional journalctl and syslog remain essential for:
- Host-level debugging when containers fail
- System service issues affecting container runtime
- Security investigations requiring host-level audit trails
- Boot and kernel issues that prevent containers from starting
Application Layer: Modern tools handle:
- Distributed application logs across multiple services
- Metrics correlation with log events
- Real-time alerting and anomaly detection
- Long-term log retention and analytics
Hybrid Debugging Workflow
Step 1 - Start with Observability Dashboards: Check Grafana dashboards for high-level service health and identify affected components.
Step 2 - Drill Down with Log Aggregation: Use Kibana or Grafana’s log panels to examine application-specific logs and error patterns.
Step 3 - Investigate Infrastructure: When application logs don’t reveal the root cause, use traditional tools:
Step 4 - Correlate Timeline: Cross-reference timestamps between modern dashboards and system logs to identify infrastructure issues affecting applications.
Practical Modern Scenarios
Kubernetes Pod Crashes:
Container Network Issues:
Resource Exhaustion Investigation:
Log Pipeline Architecture
Collection: Fluent Bit or Filebeat agents collect logs from containers and forward to centralized systems.
Processing: Logstash or Vector process, filter, and enrich logs before storage.
Storage: Elasticsearch, Loki, or cloud logging services store processed logs.
Analysis: Kibana, Grafana, or custom dashboards provide search and visualization.
Alerting: Prometheus Alertmanager or similar tools trigger notifications based on log patterns.
When to Use Which Approach
Use Traditional Methods When:
- Container runtime is failing
- Host system issues prevent modern tools from working
- Network connectivity problems affect log aggregation
- Investigating security incidents requiring audit trails
- Debugging system boot or kernel issues
Use Modern Tools When:
- Analyzing distributed application behavior
- Correlating metrics with log events
- Setting up proactive monitoring and alerting
- Investigating application performance issues
- Managing logs at scale across multiple environments
Bridging the Gap
Export journalctl to modern systems:
|
|
Configure log forwarding:
Create unified dashboards that combine traditional system metrics with application observability for complete visibility.
Understanding both traditional logging and modern observability tools gives you comprehensive insight into system behavior across the entire stack. Master these complementary approaches, and you’ll debug complex distributed systems with ninja-like precision and efficiency.