bash logging - ghdrako/doc_snipets GitHub Wiki

Log function

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."

Verbose Logging

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 }

Debug Mode

if [ "$DEBUG" = true ]; then
  set -x Enable debugging
fi
if [ "$DEBUG" = true ]; then
  set +x Disable debugging
fi

Logging Levels

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
}

Rotating Log Files

  • 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 -- {}
<your_program> 2>&1 | rotatelogs <opts> <logfile> <rotation_criteria>

logrotate

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
}

Syslog

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.

Integration with Syslog

logger -t my_script "Starting data import process" # '-t' adds a tag
logger -p user.notice "Script executed successfully."

Using logger Effectively

  1. Set Message Priority:
logger -p user.notice "Script initiated archival process"

See man logger for priority levels.

  1. 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

Centralized Logging

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.
⚠️ **GitHub.com Fallback** ⚠️