mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2024-01-19 02:48:50 +00:00
scripts: improve helpers/log.sh
(#2754)
This PR prepares for other PRs that use the newly introduced helper functions. The `_log` function itself was adjusted a bit to be shorter and more concise.
This commit is contained in:
parent
a6358ef7ef
commit
39774df85d
|
@ -46,28 +46,20 @@ function _log
|
||||||
{
|
{
|
||||||
if [[ -z ${1+set} ]]
|
if [[ -z ${1+set} ]]
|
||||||
then
|
then
|
||||||
echo "Call to '_log' is missing a valid log level" >&2
|
_log 'error' "Call to '_log' is missing a valid log level"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z ${2+set} ]]
|
if [[ -z ${2+set} ]]
|
||||||
then
|
then
|
||||||
echo "Call to '_log' is missing a message to log" >&2
|
_log 'error' "Call to '_log' is missing a message to log"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local MESSAGE LEVEL_AS_INT LOG_LEVEL_FALLBACK
|
local LEVEL_AS_INT
|
||||||
MESSAGE="${RESET}["
|
local MESSAGE="${RESET}["
|
||||||
|
|
||||||
if [[ -e /etc/dms-settings ]]
|
case "$(_get_log_level_or_default)" in
|
||||||
then
|
|
||||||
LOG_LEVEL_FALLBACK=$(grep "^LOG_LEVEL=" /etc/dms-settings | cut -d '=' -f 2)
|
|
||||||
LOG_LEVEL_FALLBACK="${LOG_LEVEL_FALLBACK:1:-1}"
|
|
||||||
else
|
|
||||||
LOG_LEVEL_FALLBACK='info'
|
|
||||||
fi
|
|
||||||
|
|
||||||
case "${LOG_LEVEL:-${LOG_LEVEL_FALLBACK}}" in
|
|
||||||
( 'trace' ) LEVEL_AS_INT=5 ;;
|
( 'trace' ) LEVEL_AS_INT=5 ;;
|
||||||
( 'debug' ) LEVEL_AS_INT=4 ;;
|
( 'debug' ) LEVEL_AS_INT=4 ;;
|
||||||
( 'warn' ) LEVEL_AS_INT=2 ;;
|
( 'warn' ) LEVEL_AS_INT=2 ;;
|
||||||
|
@ -78,12 +70,12 @@ function _log
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
( 'trace' )
|
( 'trace' )
|
||||||
[[ ${LEVEL_AS_INT} -ge 5 ]] || return 0
|
[[ ${LEVEL_AS_INT} -ge 5 ]] || return 0
|
||||||
MESSAGE+=" ${LGRAY}TRACE "
|
MESSAGE+=" ${CYAN}TRACE "
|
||||||
;;
|
;;
|
||||||
|
|
||||||
( 'debug' )
|
( 'debug' )
|
||||||
[[ ${LEVEL_AS_INT} -ge 4 ]] || return 0
|
[[ ${LEVEL_AS_INT} -ge 4 ]] || return 0
|
||||||
MESSAGE+=" ${LBLUE}DEBUG "
|
MESSAGE+=" ${PURPLE}DEBUG "
|
||||||
;;
|
;;
|
||||||
|
|
||||||
( 'info' )
|
( 'info' )
|
||||||
|
@ -98,10 +90,10 @@ function _log
|
||||||
|
|
||||||
( 'error' )
|
( 'error' )
|
||||||
[[ ${LEVEL_AS_INT} -ge 1 ]] || return 0
|
[[ ${LEVEL_AS_INT} -ge 1 ]] || return 0
|
||||||
MESSAGE+=" ${RED}ERROR " ;;
|
MESSAGE+=" ${LRED}ERROR " ;;
|
||||||
|
|
||||||
( * )
|
( * )
|
||||||
echo "Call to '_log' with invalid log level argument '${1}'" >&2
|
_log 'error' "Call to '_log' with invalid log level argument '${1}'"
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
@ -116,4 +108,32 @@ function _log
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function _log_with_date { _log "${1}" "$(date '+%Y-%m-%d %H:%M:%S') ${2}" ; }
|
# Like `_log` but adds a timestamp in front of the message.
|
||||||
|
function _log_with_date
|
||||||
|
{
|
||||||
|
_log "${1}" "$(date '+%Y-%m-%d %H:%M:%S') ${2}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get the value of the environment variable LOG_LEVEL if
|
||||||
|
# it is set. Otherwise, try to query the common environment
|
||||||
|
# variables file. If this does not yield a value either,
|
||||||
|
# use the default log level.
|
||||||
|
function _get_log_level_or_default
|
||||||
|
{
|
||||||
|
if [[ -n ${LOG_LEVEL+set} ]]
|
||||||
|
then
|
||||||
|
printf '%s' "${LOG_LEVEL}"
|
||||||
|
elif [[ -e /etc/dms-settings ]]
|
||||||
|
then
|
||||||
|
grep "^LOG_LEVEL=" /etc/dms-settings | cut -d "'" -f 2
|
||||||
|
else
|
||||||
|
printf 'info'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# This function checks whether the log level is the one
|
||||||
|
# provided as the first argument.
|
||||||
|
function _log_level_is
|
||||||
|
{
|
||||||
|
[[ $(_get_log_level_or_default) =~ ^${1}$ ]]
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue