Last active
May 27, 2020 08:58
-
-
Save molcay/fa454b653bf5719ddd567bd4fa3fcac4 to your computer and use it in GitHub Desktop.
Logging helpers for shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##### 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 ##### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment