Skip to content

Instantly share code, notes, and snippets.

@looztra
Created February 8, 2019 09:09
Show Gist options
  • Select an option

  • Save looztra/2fbba0464705b943ca53b21d9aec70ab to your computer and use it in GitHub Desktop.

Select an option

Save looztra/2fbba0464705b943ca53b21d9aec70ab to your computer and use it in GitHub Desktop.

Revisions

  1. looztra created this gist Feb 8, 2019.
    21 changes: 21 additions & 0 deletions log2stderr_with_loglevel_support.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # 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
    }