feat: Improve logging functions in backup scripts for better readability and structured output

This commit is contained in:
Peter Wood
2025-05-26 08:02:18 -04:00
parent 68f7f4ef8e
commit e90b9413de
4 changed files with 63 additions and 23 deletions

View File

@@ -170,35 +170,35 @@ log_message() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${CYAN}[${timestamp}]${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $message" >> "${LOG_FILE}" 2>/dev/null || true
echo "[${timestamp}] $message" >> "${LOG_FILE}" 2>/dev/null || true
}
log_error() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${RED}[${timestamp}] ERROR:${NC} ${message}" >&2
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $message" >> "${LOG_FILE}" 2>/dev/null || true
echo "[${timestamp}] ERROR: $message" >> "${LOG_FILE}" 2>/dev/null || true
}
log_success() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${GREEN}[${timestamp}] SUCCESS:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] SUCCESS: $message" >> "${LOG_FILE}" 2>/dev/null || true
echo "[${timestamp}] SUCCESS: $message" >> "${LOG_FILE}" 2>/dev/null || true
}
log_warning() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${YELLOW}[${timestamp}] WARNING:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $message" >> "${LOG_FILE}" 2>/dev/null || true
echo "[${timestamp}] WARNING: $message" >> "${LOG_FILE}" 2>/dev/null || true
}
log_info() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${BLUE}[${timestamp}] INFO:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] INFO: $message" >> "${LOG_FILE}" 2>/dev/null || true
echo "[${timestamp}] INFO: $message" >> "${LOG_FILE}" 2>/dev/null || true
}
# Performance tracking functions

View File

@@ -105,35 +105,35 @@ log_message() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${CYAN}[${timestamp}]${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
echo "[${timestamp}] $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
}
log_error() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${RED}[${timestamp}] ERROR:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
echo "[${timestamp}] ERROR: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
}
log_success() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${GREEN}[${timestamp}] SUCCESS:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] SUCCESS: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
echo "[${timestamp}] SUCCESS: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
}
log_warning() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${YELLOW}[${timestamp}] WARNING:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
echo "[${timestamp}] WARNING: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
}
log_info() {
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${BLUE}[${timestamp}] INFO:${NC} ${message}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] INFO: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
echo "[${timestamp}] INFO: $message" >> "${LOG_ROOT}/plex-backup-$(date '+%Y-%m-%d').log" 2>/dev/null || true
}
# Performance tracking functions

View File

@@ -17,7 +17,19 @@ MAX_INSTALL_ATTEMPTS=3
CURRENT_ATTEMPT=1
# Log file
# Create logs directory if it doesn't exist and is writable
# C log_both "\n${BLUE}Debugging lolcat package:${NC}" "Debugging lolcat package:"
if [ -f "/usr/games/lolcat" ]; then
log_both "- Lolcat found at /usr/games/lolcat"
ls -la /usr/games/lolcat >> "$LOG_FILE"
else
log_both "- Lolcat not found at /usr/games/lolcat"
fi
if check_command lolcat; then
log_both "- Lolcat found in PATH"
command -v lolcat >> "$LOG_FILE"
else
log_both "- Lolcat not found in PATH"
firectory if it doesn't exist and is writable
# First, try creating the logs directory in case it doesn't exist
if [ -d "/logs" ] || mkdir -p /logs 2>/dev/null; then
if [ -w "/logs" ]; then
@@ -52,6 +64,23 @@ echo "Starting setup test at $(date)" > "$LOG_FILE" || {
echo "Starting setup test at $(date)" > "$LOG_FILE"
}
# Helper function to log colored output to terminal and clean output to file
log_both() {
local colored_message="$1"
local clean_message="$2"
# Display colored message to terminal
echo -e "$colored_message"
# Log clean message to file
if [ -n "$clean_message" ]; then
echo "$clean_message" >> "$LOG_FILE"
else
# Strip ANSI escape codes for file logging
echo "$colored_message" | sed 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE"
fi
}
# Identify the system
echo -e "${BLUE}=== System Information ===${NC}"
if [ -f /etc/os-release ]; then
@@ -423,18 +452,18 @@ while [ $CURRENT_ATTEMPT -le $MAX_INSTALL_ATTEMPTS ]; do
;;
"cowsay")
# Extra debugging for cowsay
echo -e "\n${BLUE}Debugging cowsay package:${NC}" | tee -a "$LOG_FILE"
log_both "\n${BLUE}Debugging cowsay package:${NC}" "Debugging cowsay package:"
if [ -f "/usr/games/cowsay" ]; then
echo -e "- Cowsay found at /usr/games/cowsay" | tee -a "$LOG_FILE"
ls -la /usr/games/cowsay | tee -a "$LOG_FILE"
log_both "- Cowsay found at /usr/games/cowsay"
ls -la /usr/games/cowsay >> "$LOG_FILE"
else
echo -e "- Cowsay not found at /usr/games/cowsay" | tee -a "$LOG_FILE"
log_both "- Cowsay not found at /usr/games/cowsay"
fi
if check_command cowsay; then
echo -e "- Cowsay found in PATH" | tee -a "$LOG_FILE"
command -v cowsay | tee -a "$LOG_FILE"
log_both "- Cowsay found in PATH"
command -v cowsay >> "$LOG_FILE"
else
echo -e "- Cowsay not found in PATH" | tee -a "$LOG_FILE"
log_both "- Cowsay not found in PATH"
fi
if ! test_package "cowsay" "cowsay"; then

View File

@@ -26,23 +26,34 @@ EXPECTED_FILES=(
log_message() {
local message="$1"
echo -e "$(date '+%H:%M:%S') $message" | tee -a "$REPORT_FILE"
local clean_message="$2"
# Display colored message to terminal
echo -e "$(date '+%H:%M:%S') $message"
# Strip ANSI codes and log clean version to file
if [ -n "$clean_message" ]; then
echo "$(date '+%H:%M:%S') $clean_message" >> "$REPORT_FILE"
else
# Strip ANSI escape codes for file logging
echo "$(date '+%H:%M:%S') $message" | sed 's/\x1b\[[0-9;]*m//g' >> "$REPORT_FILE"
fi
}
log_error() {
log_message "${RED}ERROR: $1${NC}"
log_message "${RED}ERROR: $1${NC}" "ERROR: $1"
}
log_success() {
log_message "${GREEN}SUCCESS: $1${NC}"
log_message "${GREEN}SUCCESS: $1${NC}" "SUCCESS: $1"
}
log_warning() {
log_message "${YELLOW}WARNING: $1${NC}"
log_message "${YELLOW}WARNING: $1${NC}" "WARNING: $1"
}
log_info() {
log_message "${BLUE}INFO: $1${NC}"
log_message "${BLUE}INFO: $1${NC}" "INFO: $1"
}
# Check backup directory structure