Skip to content

Instantly share code, notes, and snippets.

@molcay
Last active May 27, 2020 08:58
Show Gist options
  • Save molcay/fa454b653bf5719ddd567bd4fa3fcac4 to your computer and use it in GitHub Desktop.
Save molcay/fa454b653bf5719ddd567bd4fa3fcac4 to your computer and use it in GitHub Desktop.
Logging helpers for shell
##### Logging Setup #####
# NOTE this code piece taken from: https://gist.github.com/goodmami/6556701
exec 3>&2 # logging stream (file descriptor 3) defaults to STDERR
_trc_lvl=0
_crt_lvl=1
_err_lvl=2
_wrn_lvl=3
_inf_lvl=4
_dbg_lvl=5
_lvl_str_list=(TRACE CRITICAL ERROR WARNING INFO DEBUG)
LOGGING_VERBOSITY=$_dbg_lvl
function _log_it() {
local log_lvl
log_lvl=$1
if [ "$LOGGING_VERBOSITY" -ge "$log_lvl" ]; then
local date_string
local level_str
date_string=$(date +'%Y-%m-%d %H:%M:%S')
level_str="${_lvl_str_list[$log_lvl]}"
echo -e "[$date_string] [$level_str] - $2" >&3
fi
}
function log_critical() {
_log_it "$_crt_lvl" "$1"
}
function log_error() {
_log_it "$_err_lvl" "$1"
}
function log_warning() {
_log_it "$_wrn_lvl" "$1"
}
function log_info() {
_log_it "$_inf_lvl" "$1"
}
function log_debug() {
_log_it "$_dbg_lvl" "$1"
}
function log_trace() {
_log_it "$_trc_lvl" "$1"
}
##### Logging Setup #####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment