add-sftp-user/scripts/logging.sh

131 lines
4.3 KiB
Bash

#!/bin/bash
# ==============================================================================
# Canonical Logging Library for SFTP User Addon
#
# Provides a standardized set of functions for logging operations, errors,
# debug messages, and command executions. This script is intended to be
# sourced by other scripts in the addon.
# ==============================================================================
# ------------------------------------------------------------------------------
# Log Configuration
# ------------------------------------------------------------------------------
LOG_DIR="/opt/add-sftp-user-addon/logs"
LOG_FILE="$LOG_DIR/script_output.log"
ERROR_LOG="$LOG_DIR/errors.log"
OPERATION_LOG="$LOG_DIR/operations.log"
DEBUG_LOG="$LOG_DIR/debug.log"
# Enable debug logging by setting DEBUG=1 in the environment, otherwise default to 0.
DEBUG=${DEBUG:-0}
# Ensure the log directory exists. The calling script should have permissions.
mkdir -p "$LOG_DIR" &>/dev/null
# ------------------------------------------------------------------------------
# Core Logging Engine
#
# @param {string} level - The log level (e.g., INFO, ERROR, DEBUG).
# @param {string} message - The message to log.
# ------------------------------------------------------------------------------
log_to_file() {
local level=${1:-INFO}
local message=${2}
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Generate a unique ID for each script execution for better traceability.
local script_id="$(date +%Y%m%d%H%M%S)-$$"
# Write to the main aggregated log file.
echo "[$script_id] $timestamp [$level] $message" >> "$LOG_FILE"
# Route messages to specific logs based on level.
if [[ "$level" == "ERROR" || "$level" == "WARNING" ]]; then
echo "[$script_id] $timestamp [$level] $message" >> "$ERROR_LOG"
fi
if [[ "$level" == "INFO" || "$level" == "SUCCESS" ]]; then
echo "[$script_id] $timestamp [$level] $message" >> "$OPERATION_LOG"
fi
if [[ "$level" == "DEBUG" && "$DEBUG" -eq 1 ]]; then
echo "[$script_id] $timestamp [$level] $message" >> "$DEBUG_LOG"
fi
}
# ------------------------------------------------------------------------------
# Logging Helper Functions
# ------------------------------------------------------------------------------
# Logs an informational message.
log() {
log_to_file "INFO" "$1"
}
# Logs an error message.
log_error() {
log_to_file "ERROR" "$1"
}
# Logs a warning message.
log_warning() {
log_to_file "WARNING" "$1"
}
# Logs a debug message, only if DEBUG is enabled.
log_debug() {
if [ "$DEBUG" -eq 1 ]; then
log_to_file "DEBUG" "$1"
fi
}
# Logs a success message.
log_success() {
log_to_file "SUCCESS" "$1"
}
# ------------------------------------------------------------------------------
# System & Command Logging
# ------------------------------------------------------------------------------
# Logs critical system information for debugging context.
log_system_info() {
log_debug "============= SYSTEM INFORMATION ============="
log_debug "Operating System: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | tr -d '\"')"
log_debug "Kernel: $(uname -r)"
log_debug "SSH Version: $(ssh -V 2>&1)"
log_debug "SSH Config Status: $(systemctl status sshd | grep Active | awk '{print $2}')"
# Note: Specific script parameters should be logged by the calling script.
log_debug "=============================================="
}
# Executes a command, captures its output and status, and logs the result.
#
# @param {string} cmd - The command to execute.
# @param {string} cmd_desc - A human-readable description of the command.
# @returns {int} The exit status of the executed command.
# ------------------------------------------------------------------------------
log_cmd() {
local cmd="$1"
local cmd_desc="$2"
log_debug "Executing: $cmd_desc"
log_debug "Command: $cmd"
# Execute command and capture combined stdout/stderr.
local output
output=$(eval "$cmd" 2>&1)
local status=$?
if [ $status -eq 0 ]; then
log_debug "Command succeeded: $cmd_desc"
if [ -n "$output" ]; then
log_debug "Output: $output"
fi
else
log_error "Command failed with status $status: $cmd_desc"
log_error "Error output: $output"
fi
return $status
}