Files
gniza4cp/lib/logging.sh
shuki 1459bd1b8b Initial commit: gniza backup & disaster recovery CLI + WHM plugin
Full-featured cPanel backup tool with SSH, S3, and Google Drive support.
Includes WHM plugin with Tailwind/DaisyUI UI, multi-remote management,
decoupled schedules, and account restore workflows.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 02:39:39 +02:00

58 lines
1.7 KiB
Bash

#!/usr/bin/env bash
# gniza/lib/logging.sh — Per-run log files, log_info/warn/error/debug
declare -g LOG_FILE=""
_log_level_num() {
case "$1" in
debug) echo 0 ;;
info) echo 1 ;;
warn) echo 2 ;;
error) echo 3 ;;
*) echo 1 ;;
esac
}
init_logging() {
local log_dir="${LOG_DIR:-$DEFAULT_LOG_DIR}"
mkdir -p "$log_dir" || die "Cannot create log directory: $log_dir"
LOG_FILE="$log_dir/gniza-$(date -u +%Y%m%d-%H%M%S).log"
touch "$LOG_FILE" || die "Cannot write to log file: $LOG_FILE"
# Clean old logs
local retain="${LOG_RETAIN:-$DEFAULT_LOG_RETAIN}"
find "$log_dir" -name "gniza-*.log" -mtime +"$retain" -delete 2>/dev/null || true
}
_log() {
local level="$1"; shift
local msg="$*"
local configured_level="${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}"
local level_num; level_num=$(_log_level_num "$level")
local configured_num; configured_num=$(_log_level_num "$configured_level")
(( level_num < configured_num )) && return 0
local ts; ts=$(date -u +"%Y-%m-%d %H:%M:%S")
local upper; upper=$(echo "$level" | tr '[:lower:]' '[:upper:]')
local line="[$ts] [$upper] $msg"
# Always write to log file if initialized
[[ -n "$LOG_FILE" ]] && echo "$line" >> "$LOG_FILE"
# Print to stderr based on level
case "$level" in
error) echo "${C_RED}${line}${C_RESET}" >&2 ;;
warn) echo "${C_YELLOW}${line}${C_RESET}" >&2 ;;
info) echo "${line}" >&2 ;;
debug) echo "${C_BLUE}${line}${C_RESET}" >&2 ;;
esac
}
log_info() { _log info "$@"; }
log_warn() { _log warn "$@"; }
log_error() { _log error "$@"; }
log_debug() { _log debug "$@"; }