| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | #!/bin/bash -e# log helper base on environment variable CONTAINER_LOG_LEVEL# CONTAINER_LOG_LEVEL environment variable is set by run tool based on --log-level argument (info by default)# or you can set it directly with docker --env argument# Usage example: log-helper info CONTAINER_LOG_LEVEL is info or more# the message "CONTAINER_LOG_LEVEL is info or more" will be printed only if log level is info, debug or traceLOG_LEVEL_NONE=0LOG_LEVEL_ERROR=1LOG_LEVEL_WARNING=2LOG_LEVEL_INFO=3LOG_LEVEL_DEBUG=4LOG_LEVEL_TRACE=5# default log level if CONTAINER_LOG_LEVEL is not set -> infolog_level=${CONTAINER_LOG_LEVEL:-${LOG_LEVEL_INFO}}call=$1 # function to call (error, warning, info, debug, trace, level)if [[ ! "$call" =~ ^(error|warning|info|debug|trace|level)$ ]]; then    echo "Error: Function $call not found"    echo "Allowed functions are: error, warning, info, debug, trace, level"    echo "usage example: log-helper info hello !"    exit 1fiecho_msg="" # message to print if required log level is setecho_param="" # echo command parametersfunction print_log(){    local level_txt=$1    local message=$2    local date=$(date +"%Y-%m-%d %T")    readarray -t messages <<<"$message"    for m in "${messages[@]}"; do        echo "*** ${level_txt} | ${date} | ${m}"    done}function error() {    # getEchoParams no matter what level it is to not break pipes    getEchoParams $@    if [ $log_level -ge 1 ]; then        echo $echo_param  "$(print_log " ERROR " "$echo_msg")"    fi}function warning() {    # getEchoParams no matter what level it is to not break pipes    getEchoParams $@    if [ $log_level -ge 2 ]; then        echo $echo_param  "$(print_log "WARNING" "$echo_msg")"    fi}function info() {    # getEchoParams no matter what level it is to not break pipes    getEchoParams $@    if [ $log_level -ge 3 ]; then        echo $echo_param "$(print_log " INFO  " "$echo_msg")"    fi}function debug() {    # getEchoParams no matter what level it is to not break pipes    getEchoParams $@    if [ $log_level -ge 4 ]; then        echo $echo_param "$(print_log " DEBUG " "$echo_msg")"    fi}function trace() {    # getEchoParams no matter what level it is to not break pipes    getEchoParams $@    if [ $log_level -ge 5 ]; then        echo $echo_param "$(print_log " TRACE " "$echo_msg")"    fi}function getMsgFromStdin() {    if [ -z "$2" ]; then        echo_msg=$(cat)    fi}function getEchoParams() {        echo_msg="$@"        if [[ "$1" =~ ^(-e|-n|-E)$ ]]; then        echo_param=$1        echo_msg=${echo_msg#$1 }    fi        # read from pipe if echo_msg is empty    [[ -n "$echo_msg" ]] || getMsgFromStdin}function level() {        local operator=$1    local loglevel_str=$2    local loglevel_str=${loglevel_str^^} # uppercase        if [[ ! "$operator" =~ ^(eq|ne|gt|ge|lt|le)$ ]]; then        echo "Error: Operator $operator not allowed"        echo "Allowed operators are: eq, ne, gt, ge, lt, le"        echo "Help: http://www.tldp.org/LDP/abs/html/comparison-ops.html"        exit 1    fi        if [ -z "$loglevel_str" ]; then        echo "Error: No log level provided"        echo "Allowed log level are: none, error, warning, info, debug, trace"        echo "usage example: log-helper level eq info"        exit 1    fi        local log_level_var=LOG_LEVEL_$loglevel_str        if [ $log_level -$operator ${!log_level_var} ]; then        exit 0    else        exit 1    fi}shift$call "$@"
 |