Skip to content

Instantly share code, notes, and snippets.

@molcay
Last active May 27, 2020 08:58
Show Gist options
  • Select an option

  • Save molcay/fa454b653bf5719ddd567bd4fa3fcac4 to your computer and use it in GitHub Desktop.

Select an option

Save molcay/fa454b653bf5719ddd567bd4fa3fcac4 to your computer and use it in GitHub Desktop.

Revisions

  1. molcay revised this gist May 27, 2020. 1 changed file with 2 additions and 12 deletions.
    14 changes: 2 additions & 12 deletions logging.sh
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,7 @@ function _log_it() {
    local level_str
    date_string=$(date +'%Y-%m-%d %H:%M:%S')
    level_str="${_lvl_str_list[$log_lvl]}"
    # Expand escaped characters, wrap at 70 chars, indent wrapped lines
    echo -e "[$date_string] [$level_str] - $2" | fold -w 80 -s | sed '2~1s/^/ /' >&3
    echo -e "[$date_string] [$level_str] - $2" >&3
    fi
    }

    @@ -48,13 +47,4 @@ function log_debug() {
    function log_trace() {
    _log_it "$_trc_lvl" "$1"
    }
    ##### Logging Setup #####

    ##### Sample #####
    log_trace "TRACE log entry"
    log_debug "DEBUG log entry"
    log_info "INFO log entry"
    log_warning "WARNING log entry"
    log_error "ERROR log entry"
    log_critical "CRITICAL log entry"
    ##### Sample #####
    ##### Logging Setup #####
  2. molcay revised this gist May 27, 2020. 1 changed file with 18 additions and 5 deletions.
    23 changes: 18 additions & 5 deletions logging.sh
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,27 @@
    ##### 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
    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=(CRITICAL ERROR WARNING INFO DEBUG)
    _lvl_str_list=(TRACE CRITICAL ERROR WARNING INFO DEBUG)
    LOGGING_VERBOSITY=$_dbg_lvl

    function _log_it() {
    local log_lvl
    log_lvl=$1
    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]}"
    # Expand escaped characters, wrap at 70 chars, indent wrapped lines
    echo -e "[$date_string] [$level_str] - $2" >&3
    echo -e "[$date_string] [$level_str] - $2" | fold -w 80 -s | sed '2~1s/^/ /' >&3
    fi
    }

    @@ -44,4 +44,17 @@ function log_info() {
    function log_debug() {
    _log_it "$_dbg_lvl" "$1"
    }
    ##### Logging Setup #####

    function log_trace() {
    _log_it "$_trc_lvl" "$1"
    }
    ##### Logging Setup #####

    ##### Sample #####
    log_trace "TRACE log entry"
    log_debug "DEBUG log entry"
    log_info "INFO log entry"
    log_warning "WARNING log entry"
    log_error "ERROR log entry"
    log_critical "CRITICAL log entry"
    ##### Sample #####
  3. molcay created this gist May 5, 2020.
    47 changes: 47 additions & 0 deletions logging.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    ##### 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

    _crt_lvl=1
    _err_lvl=2
    _wrn_lvl=3
    _inf_lvl=4
    _dbg_lvl=5
    _lvl_str_list=(CRITICAL ERROR WARNING INFO DEBUG)
    LOGGING_VERBOSITY=$_dbg_lvl

    function _log_it() {
    local log_lvl
    log_lvl=$1
    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]}"
    # Expand escaped characters, wrap at 70 chars, indent wrapped lines
    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"
    }
    ##### Logging Setup #####