Practical Logging Skills
Custom Logging Techniques
Creating Custom Log Messages
## Using logger command for custom logging
logger -p user.info "LabEx custom log message"
## Logging with priority and tag
logger -t MyApplication -p local0.warning "System performance alert"
Log Filtering and Analysis
Advanced Log Searching
## Filter logs by specific criteria
grep "ERROR" /var/log/syslog
## Complex log filtering
journalctl -p err -b ## Show error-level messages from current boot
Log Analysis Workflow
graph LR
A[Log Collection] --> B[Filtering]
B --> C[Pattern Matching]
C --> D[Analysis]
D --> E[Reporting]
## Capture system performance logs
sar -u 1 5 ## CPU utilization every second, 5 times
Log Parsing Techniques
Using awk for Log Processing
## Extract specific log columns
awk '{print $5, $6}' /var/log/syslog
## Count occurrences of log entries
awk '/error/ {count++} END {print "Error count:", count}' /var/log/syslog
Tool |
Purpose |
Complexity |
Performance |
syslog |
Basic logging |
Low |
Medium |
rsyslog |
Advanced logging |
Medium |
High |
journalctl |
SystemD logging |
Medium |
High |
Security Log Monitoring
Intrusion Detection Logging
## Monitor authentication attempts
grep "Failed password" /var/log/auth.log
## Real-time SSH login monitoring
tail -f /var/log/auth.log | grep sshd
Automated Log Analysis Script
#!/bin/bash
## LabEx Log Analysis Utility
LOG_FILE="/var/log/syslog"
## Function to analyze log severity
analyze_log_severity() {
echo "Critical Errors:"
grep -E "error|critical" "$LOG_FILE" | wc -l
}
## Function to track unique IP addresses
track_ip_connections() {
echo "Unique IP Connections:"
grep -oP '(\d{1,3}\.){3}\d{1,3}' "$LOG_FILE" | sort | uniq -c
}
## Main execution
main() {
echo "Log Analysis Report"
analyze_log_severity
track_ip_connections
}
main
Advanced Logging Strategies
- Implement centralized logging
- Use log aggregation tools
- Set up real-time alerts
- Implement log rotation
- Encrypt sensitive logs
Logging Best Practices
Configuration Management
## Backup existing log configurations
cp /etc/rsyslog.conf /etc/rsyslog.conf.backup
## Test log configuration
rsyslogd -N1
## Check log file size
du -h /var/log/syslog
## Monitor log growth
watch -n 10 "du -h /var/log/syslog"
By mastering these practical logging skills, you'll become proficient in managing, analyzing, and securing system logs in your Linux environment.