Skip to content

Instantly share code, notes, and snippets.

@looztra
Created February 8, 2019 09:09
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 looztra/2fbba0464705b943ca53b21d9aec70ab to your computer and use it in GitHub Desktop.
Save looztra/2fbba0464705b943ca53b21d9aec70ab to your computer and use it in GitHub Desktop.
# Borrowed to someone, but I don't remember who it was, sorry :(
# Print message $2 with log-level $1 to STDERR, colorized if terminal
# log DEBUG "DOCKER_HOST ${DOCKER_HOST}"
log() {
local level=${1?}
shift
local code= line="[$(date '+%F %T')] $level: $*"
if [ -t 2 ]
then
case "$level" in
INFO) code=36 ;;
DEBUG) code=35 ;;
WARN) code=33 ;;
ERROR) code=31 ;;
*) code=37 ;;
esac
echo -e "\e[${code}m${line}\e[0m"
else
echo "$line"
fi >&2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment