Last active
March 26, 2019 13:15
-
-
Save RopoMen/1e81bd65b3766ab6e1f2a7c45f8f6aeb to your computer and use it in GitHub Desktop.
Set of logging helper functions that simplifies bash logging. Session ID helps to identify log rows that belong to same session and allows multiple scripts to use same logfile.
This file contains 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
# For OSX https://unix.stackexchange.com/questions/141420/tr-complains-of-illegal-byte-sequence | |
export LC_ALL=C; | |
# https://gist.github.com/earthgecko/3089509 | |
INTERNAL_SESSION_ID="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)" | |
function get_session_id() { | |
echo "$INTERNAL_SESSION_ID" | |
} | |
function get_logdate() { | |
echo "$(date '+%Y-%m-%dT%H:%M:%S')" | |
} | |
# When "logging_utils.sh" is sourced in to another script $0 will contain name of that script, not "logging_utils.sh" | |
function get_script_name() { | |
echo "$(basename $0)" | |
} | |
# Arguments: | |
# $1 - log level | |
# $2 - log message | |
# $3 - echo arguments | |
function log() { | |
echo "$3" "$(get_logdate) $1 - $(get_session_id) $(get_script_name): $2" | |
} | |
# Arguments: | |
# $1 Log message | |
# $2 echo arguments | |
function log_debug() { | |
log "DEBUG" "$1" "$2" | |
} | |
# Arguments: | |
# $1 Log message | |
# $2 echo arguments | |
function log_info() { | |
log "INFO" "$1" "$2" | |
} | |
# Arguments: | |
# $1 Log message | |
# $2 echo arguments | |
function log_warn() { | |
log "WARN" "$1" "$2" | |
} | |
# Arguments: | |
# $1 Log message | |
# $2 echo arguments | |
function log_error() { | |
log "ERROR" "$1" "$2" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment