platform-tooling/scripts/lib/logger.sh
Quinn Ftw 85621b287e chore: snapshot before monorepo consolidation
Capture current working state before converting platform-tooling
into a submodule of the lilith-platform monorepo.
2026-01-29 07:04:39 -08:00

100 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
#
# Unified Logging Functions
# Provides consistent logging across all infrastructure scripts
#
# Usage:
# source lib/logger.sh
# log_init "MY-PREFIX"
# log_info "Message"
#
# Dependencies: lib/colors.sh
#
# Ensure colors are loaded
SCRIPT_LIB_DIR="${SCRIPT_LIB_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
source "${SCRIPT_LIB_DIR}/colors.sh"
# Log prefix (set via log_init)
_LOG_PREFIX=""
# Initialize logging with a prefix
log_init() {
local prefix="${1:-SCRIPT}"
_LOG_PREFIX="[${prefix}]"
}
# Core logging function
_log() {
local level="$1"
local color="$2"
local message="$3"
local use_syslog="${4:-false}"
echo -e "${color}${_LOG_PREFIX}${COLOR_NC} ${message}"
# Also log to syslog if requested
if [ "$use_syslog" = "true" ] && command -v logger &>/dev/null; then
local syslog_priority="user.info"
case "$level" in
warn) syslog_priority="user.warning" ;;
error) syslog_priority="user.err" ;;
esac
logger -t "${_LOG_PREFIX//[\[\]]/}" -p "$syslog_priority" "$message"
fi
}
# Logging functions
log_info() {
_log "info" "$COLOR_SUCCESS" "$1" "${2:-false}"
}
log_warn() {
_log "warn" "$COLOR_WARNING" "$1" "${2:-false}"
}
log_error() {
_log "error" "$COLOR_ERROR" "$1" "${2:-false}"
}
log_step() {
_log "step" "$COLOR_INFO" "$1" "${2:-false}"
}
log_debug() {
if [ "${DEBUG:-false}" = "true" ]; then
_log "debug" "$COLOR_DEBUG" "$1" "${2:-false}"
fi
}
# Formatted outputs
log_success() {
log_info "$1"
}
log_failure() {
log_error "$1"
}
log_warning_icon() {
log_warn "⚠️ $1"
}
# Section headers
log_section() {
local title="$1"
echo ""
log_info "════════════════════════════════════════════════════"
log_info " $title"
log_info "════════════════════════════════════════════════════"
echo ""
}
log_banner() {
local title="$1"
echo ""
log_info "╔══════════════════════════════════════════════════════════════╗"
log_info "${title}"
log_info "╚══════════════════════════════════════════════════════════════╝"
echo ""
}