bash logging - ghdrako/doc_snipets GitHub Wiki
- rsyslog Configuration guide: https://www.rsyslog.com/doc/v8-stable/
- Structured Logging: https://stackify.com/what-is-structured-logging-and-why-developers-need-it/
log_file="/var/log/my_backup.log"
log() {
local message="$1"
local log_file="script.log"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$log_file"
}
# Usage
log "Script started."
Provide a verbose mode that logs additional information for in-depth debugging. verbose_log() { [ "$VERBOSE" = true ] && echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> verbose.log }
if [ "$DEBUG" = true ]; then
set -x Enable debugging
fi
if [ "$DEBUG" = true ]; then
set +x Disable debugging
fi
log_info() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO: $1" >> script.log
}
log_warning() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - WARNING: $1" >> script.log
}
log_error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1" >> script.log
}
- implementation
log_file="script.log"
max_logs=5
if [ -f "$log_file" ]; then
mv "$log_file" "$log_file.1"
fi
ls -1 "$log_file". | sort -r -n | tail -n +$max_logs | xargs -I {} rm -- {}
- Apache's rotatelogs:
<your_program> 2>&1 | rotatelogs <opts> <logfile> <rotation_criteria>
Example logrotate
config snippet for your Script:
/var/log/my_script.log {
daily
rotate 14 # Keep 2 weeks of history
compress
missingok # No fuss if the log file isn't there yet
}
Most Linux systems have a logging daemon (rsyslogd or syslog-ng) configured to:
- Categorize Logs: Messages tagged with facilities (auth, cron, user, etc.) and severity levels (info, warn, crit)
- Route Log Traffic: To different files, network targets, even databases.
- Linux Logging Basics - Syslog, rsyslog, Journald: https://linux-audit.com/linux-logging-basics/
- The logger command on Linux https://man7.org/linux/man-pages/man1/logger.1.html This command-line tool standardizes sending messages to your system’s logging facility (syslogd on most Linux systems).
logger -t my_script "Starting data import process" # '-t' adds a tag
logger -p user.notice "Script executed successfully."
- Set Message Priority:
logger -p user.notice "Script initiated archival process"
See man logger for priority levels.
- Tagging (Important!):
logger -t disk_cleanup -s "Cleaned 10.5GB of old log files"
# '-s' also prints to STDOUT, great for live status display
Tags give you log filters to see just your script’s output
For multiple machines, send logs over the network:
- Remote Syslog Server: Can receive messages over UDP, TCP (reliable, use for mission-critical stuff). Your rsyslog config defines which messages it accepts.
- ELK/EFK or Graylog: Popular log aggregation platforms. Great when lots of different sources need management, analysis, and dashboards.