Skip to content

Instantly share code, notes, and snippets.

@utisam
Created May 14, 2019 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utisam/447ed9bef71c6d8cf068d4d2295b1bde to your computer and use it in GitHub Desktop.
Save utisam/447ed9bef71c6d8cf068d4d2295b1bde to your computer and use it in GitHub Desktop.
Bash Logger
#!/bin/bash
[[ -n "${__LOGGER_SH:-}" ]] && return || readonly __LOGGER_SH=1
# Usage:
# source ./logger.sh
# log_info "message"
# 0: DEBUG
# 1: INFO
# 2: WARN
# 3: ERROR
declare LOG_LEVEL=${LOG_LEVEL:-1}
log_debug() {
if (( $LOG_LEVEL <= 0 )); then
echo -e "\033[1;40m DEBUG \033[00m $*" 1>&2
fi
}
log_info() {
if (( $LOG_LEVEL <= 1 )); then
echo -e "\033[1;44m INFO \033[00m $*" 1>&2
fi
}
log_warn() {
if (( $LOG_LEVEL <= 2 )); then
echo -e "\033[1;43m WARN \033[00m $*" 1>&2
fi
}
log_error() {
if (( $LOG_LEVEL <= 3 )); then
echo -e "\033[1;41m ERROR \033[00m $*" 1>&2
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment