Add conditional logging based on logging_level

This commit is contained in:
icenine451 2024-10-18 09:40:28 -04:00
parent ef9b5024fd
commit a5eba709d9

View file

@ -41,42 +41,50 @@ log() {
case "$level" in case "$level" in
d) d)
if [ "$colorize_terminal" = true ]; then if [[ $logging_level == "debug" ]]; then
# Debug (green) for terminal if [ "$colorize_terminal" = true ]; then
colored_message="\e[32m[DEBUG] $message\e[0m" # Debug (green) for terminal
else colored_message="\e[32m[DEBUG] $message\e[0m"
# Debug (no color for sh) for terminal else
colored_message="$timestamp [DEBUG] $message" # Debug (no color for sh) for terminal
colored_message="$timestamp [DEBUG] $message"
fi
# Write to log file without colorization
log_message="$timestamp [DEBUG] $message"
fi fi
# Write to log file without colorization
log_message="$timestamp [DEBUG] $message"
;; ;;
e) e)
if [ "$colorize_terminal" = true ]; then if [[ $logging_level == "debug" || $logging_level == "error" ]]; then
# Error (red) for terminal if [ "$colorize_terminal" = true ]; then
colored_message="\e[31m[ERROR] $message\e[0m" # Error (red) for terminal
else colored_message="\e[31m[ERROR] $message\e[0m"
# Error (no color for sh) for terminal else
colored_message="$timestamp [ERROR] $message" # Error (no color for sh) for terminal
colored_message="$timestamp [ERROR] $message"
fi
# Write to log file without colorization
log_message="$timestamp [ERROR] $message"
fi fi
# Write to log file without colorization
log_message="$timestamp [ERROR] $message"
;; ;;
w) w)
if [ "$colorize_terminal" = true ]; then if [[ $logging_level == "debug" || $logging_level == "error" || $logging_level == "warn" ]]; then
# Warning (yellow) for terminal if [ "$colorize_terminal" = true ]; then
colored_message="\e[33m[WARN] $message\e[0m" # Warning (yellow) for terminal
else colored_message="\e[33m[WARN] $message\e[0m"
# Warning (no color for sh) for terminal else
colored_message="$timestamp [WARN] $message" # Warning (no color for sh) for terminal
colored_message="$timestamp [WARN] $message"
fi
# Write to log file without colorization
log_message="$timestamp [WARN] $message"
fi fi
# Write to log file without colorization
log_message="$timestamp [WARN] $message"
;; ;;
i) i)
# Write to log file without colorization for info message if [[ $logging_level == "debug" || $logging_level == "error" || $logging_level == "warn" || $logging_level == "info" ]]; then
log_message="$timestamp [INFO] $message" # Write to log file without colorization for info message
colored_message=$log_message log_message="$timestamp [INFO] $message"
colored_message=$log_message
fi
;; ;;
*) *)
# Default (no color for other shells) for terminal # Default (no color for other shells) for terminal