Skip to content

Instantly share code, notes, and snippets.

@leighklotz
Last active April 3, 2025 21:23
Show Gist options
  • Select an option

  • Save leighklotz/5ce3f1b5e195d846f6bb6e022bd4cb52 to your computer and use it in GitHub Desktop.

Select an option

Save leighklotz/5ce3f1b5e195d846f6bb6e022bd4cb52 to your computer and use it in GitHub Desktop.
Bash Logging
#!/bin/bash
# Check if the script is being sourced or directly executed
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This script is intended to be sourced, not executed directly." >> /dev/stderr
exit 1
fi
COLOR_RED='\e[1;31m'
COLOR_YELLOW='\e[1;33m'
COLOR_GREEN='\e[1;32m'
COLOR_BLUE='\e[1;36m'
NOCOLOR='\e[0m'
# RWK: https://gist.github.com/akostadinov/33bb2606afe1b334169dfbf202991d36?permalink_comment_id=4962266#gistcomment-4962266
function stack_trace() {
local status_code="${1}"
local -a stack=("Stack trace of error code '${status_code}':")
local stack_size=${#FUNCNAME[@]}
local -i i
local indent=" "
# to avoid noise we start with 1 to skip the stack function
for (( i = 1; i < stack_size; i++ )); do
local func="${FUNCNAME[$i]:-(top level)}"
local -i line="${BASH_LINENO[$(( i - 1 ))]}"
local src="${BASH_SOURCE[$i]:-(no file)}"
stack+=("$indent β”” $src:$line ($func)")
indent="${indent} "
done
(IFS=$'\n'; echo "${stack[*]}")
}
function log_with_icon {
local icon="$1"
local message="$2"
local timestamp="$(date -u +"%Y-%m-%d %H:%M:%S.%3NZ")"
printf "%s %s %b\n" "${icon}" "${timestamp}" "${message}" > /dev/stderr
}
function log_verbose {
local prog="$(basename "$0")"
local message="$1"
if [ -n "${VERBOSE}" ]; then
log_with_icon "πŸ“£" "${prog}: ${message}"
fi
}
function log_debug {
local prog="$(basename "$0")"
local message="$1"
if [ -n "${DEBUG}" ]; then
log_with_icon "🐞" "${COLOR_BLUE}${prog}:${NOCOLOR} ${message}"
fi
}
function log_info {
local prog="$(basename "$0")"
local message="$1"
if [ -n "${INFO}" ]; then
log_with_icon "βœ…" "${COLOR_GREEN}INFO ${prog}:${NOCOLOR} ${message}"
fi
}
function log_warn {
local prog="$(basename "$0")"
local code=$1
local message="$2"
log_with_icon "⚠️" "${COLOR_YELLOW}WARN ${prog} (${code}):${NOCOLOR} ${message}"
}
function log_error {
local prog="$(basename "$0")"
local message="$1"
local code=$?
log_with_icon "❌" "${COLOR_RED}ERROR in ${prog}:${NOCOLOR} ${message}"
[ -n "${PRINT_STACK_TRACE}" ] && printf "%s\n" "$(stack_trace $code)" > /dev/stderr
}
function log_and_exit {
local prog="$(basename "$0")"
local code="$1"
local message="$2"
log_with_icon "β›”" "${COLOR_RED}ERROR in ${prog}:${NOCOLOR} ${message}"
[ -n "${PRINT_STACK_TRACE}" ] && printf "%s\n" "$(stack_trace "$code")" > /dev/stderr
[[ "${code}" =~ ^[0-9]+$ ]] && exit "${code}" || exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment