Improve logging and error handling with session IDs

Introduces a SESSION_ID variable for log file naming and tracking, updates log file paths to include timestamps and session IDs, and enhances error handling output to use custom message functions when available. Also improves log file management and user guidance for viewing logs, and refactors error handler to better support containerized environments.
This commit is contained in:
CanbiZ 2025-11-04 16:59:02 +01:00
parent a6cdb474a1
commit b55e8f5f34
3 changed files with 144 additions and 105 deletions

View File

@ -27,7 +27,8 @@ variables() {
DIAGNOSTICS="yes" # sets the DIAGNOSTICS variable to "yes", used for the API call.
METHOD="default" # sets the METHOD variable to "default", used for the API call.
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" # generates a random UUID and sets it to the RANDOM_UUID variable.
CTTYPE="${CTTYPE:-${CT_TYPE:-1}}"
SESSION_ID="${RANDOM_UUID:0:8}" # Short session ID (first 8 chars of UUID) for log files
CTTYPE="${CTTYPE:-${CT_TYPE:-1}}"}
# Get Proxmox VE version and kernel version
if command -v pveversion >/dev/null 2>&1; then
@ -2144,6 +2145,7 @@ build_container() {
fi
export DIAGNOSTICS="$DIAGNOSTICS"
export RANDOM_UUID="$RANDOM_UUID"
export SESSION_ID="$SESSION_ID"
export CACHER="$APT_CACHER"
export CACHER_IP="$APT_CACHER_IP"
export tz="$timezone"

View File

@ -108,7 +108,7 @@ set_std_mode() {
fi
}
SILENT_LOGFILE="/tmp/silent.$$.log"
SILENT_LOGFILE="/tmp/install-$(date +%Y%m%d_%H%M%S)_${SESSION_ID:-$(date +%s)}.log"
silent() {
local cmd="$*"
@ -133,8 +133,8 @@ silent() {
explanation="$(explain_exit_code "$rc")"
printf "\e[?25h"
echo -e "\n${RD}[ERROR]${CL} in line ${RD}${caller_line}${CL}: exit code ${RD}${rc}${CL} (${explanation})"
echo -e "${RD}Command:${CL} ${YWB}${cmd}${CL}\n"
msg_error "in line ${caller_line}: exit code ${rc} (${explanation})"
msg_custom "→" "${YWB}" "${cmd}"
if [[ -s "$SILENT_LOGFILE" ]]; then
local log_lines=$(wc -l <"$SILENT_LOGFILE")
@ -144,7 +144,7 @@ silent() {
# Show how to view full log if there are more lines
if [[ $log_lines -gt 10 ]]; then
echo -e "${YW}View full log (${log_lines} lines):${CL} cat $SILENT_LOGFILE"
msg_custom "📋" "${YW}" "View full log (${log_lines} lines): /tmp/install-*_${SESSION_ID:-*}.log"
fi
fi

View File

@ -95,7 +95,13 @@ error_handler() {
explanation="$(explain_exit_code "$exit_code")"
printf "\e[?25h"
# Use msg_error if available, fallback to echo
if declare -f msg_error >/dev/null 2>&1; then
msg_error "in line ${line_number}: exit code ${exit_code} (${explanation}): while executing command ${command}"
else
echo -e "\n${RD}[ERROR]${CL} in line ${RD}${line_number}${CL}: exit code ${RD}${exit_code}${CL} (${explanation}): while executing command ${YWB}${command}${CL}\n"
fi
if [[ -n "${DEBUG_LOGFILE:-}" ]]; then
{
@ -109,9 +115,32 @@ error_handler() {
fi
if [[ -n "${SILENT_LOGFILE:-}" && -s "$SILENT_LOGFILE" ]]; then
echo "--- Last 20 lines of silent log ($SILENT_LOGFILE) ---"
echo "--- Last 20 lines of silent log ---"
tail -n 20 "$SILENT_LOGFILE"
echo "---------------------------------------------------"
echo "-----------------------------------"
if [[ -n "${CTID:-}" ]]; then
local HOST_LOG="/tmp/install-$(date +%Y%m%d_%H%M%S)_${SESSION_ID:-error}.log"
if pct push $CTID "$SILENT_LOGFILE" "$HOST_LOG" 2>/dev/null; then
if declare -f msg_custom >/dev/null 2>&1; then
msg_custom "✓" "${GN}" "Full log saved to host: ${HOST_LOG}"
else
echo -e "${GN}✓ Full log saved to host:${CL} ${BL}${HOST_LOG}${CL}"
fi
else
if declare -f msg_custom >/dev/null 2>&1; then
msg_custom "📋" "${YW}" "Full log path in container: ${SILENT_LOGFILE}"
else
echo -e "${YW}Full log path in container:${CL} ${BL}${SILENT_LOGFILE}${CL}"
fi
fi
else
if declare -f msg_custom >/dev/null 2>&1; then
msg_custom "📋" "${YW}" "Full log: ${SILENT_LOGFILE}"
else
echo -e "${YW}Full log:${CL} ${BL}${SILENT_LOGFILE}${CL}"
fi
fi
fi
exit "$exit_code"
@ -126,12 +155,20 @@ on_exit() {
# === Signal handlers ==========================================================
on_interrupt() {
if declare -f msg_error >/dev/null 2>&1; then
msg_error "Interrupted by user (SIGINT)"
else
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
fi
exit 130
}
on_terminate() {
if declare -f msg_error >/dev/null 2>&1; then
msg_error "Terminated by signal (SIGTERM)"
else
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
fi
exit 143
}