Logging Best Practices
Structured Logging in BASH Scripts¶
Effective logging is critical for debugging, monitoring, and auditing BASH scripts. Structured logging ensures consistency, readability, and scalability. This section covers best practices for implementing timestamps, log levels, and file rotation in your scripts.
1. Use Timestamps for Contextual Logging¶
Timestamps help correlate log entries with system events and simplify troubleshooting. Always include a consistent timestamp format in your logs.
Example:
log_message() {
local level="$1"
local message="$2"
echo "$(date +'%Y-%m-%d %H:%M:%S') [${level}] ${message}"
}
Usage:
Output:
Tip: Use date +'%Y-%m-%d %H:%M:%S' for ISO 8601-compliant timestamps.
2. Implement Log Levels for Prioritization¶
Define log levels (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL) to categorize the severity of events. This allows filtering and prioritizing logs during analysis.
Example:
log_debug() { log_message "DEBUG" "$@"; }
log_info() { log_message "INFO" "$@"; }
log_error() { log_message "ERROR" "$@"; }
Usage:
Tip: Use set -x for debug output in scripts, but avoid relying on it for production logging.
3. Rotate Logs to Prevent Disk Usage¶
Large log files can consume disk space. Use tools like logrotate (systemd-based) or manual rotation with split/gzip to manage log size.
Example: logrotate Configuration
Create /etc/logrotate.d/myscript:
Manual Rotation Example:
# Rotate logs by size
split -b 1M /var/log/script.log /var/log/script.log.
gzip /var/log/script.log.*
Tip: Combine rotation with timestamped filenames (e.g., script-$(date +%Y%m%d).log) for easier archiving.
4. Secure and Centralize Log Storage¶
Store logs in a dedicated directory (e.g., /var/log/myscript/) and set appropriate permissions:
Tip: Use rsyslog or syslog-ng to send logs to a centralized server for monitoring.
5. Use Colorized Output for Debugging¶
Enhance readability during development with ANSI color codes:
log_info() {
echo -e "\e[34m[INFO] \e[0m${1}" # Blue text
}
log_error() {
echo -e "\e[31m[ERROR] \e[0m${1}" # Red text
}
Key takeaways¶
- Always include timestamps in logs for contextual clarity.
- Use standardized log levels to prioritize and filter messages.
- Rotate logs regularly to prevent disk exhaustion.
- Secure log files with proper permissions and centralized storage.
- Leverage colorized output for faster debugging during development.