scripts: update log format (#4035)

Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
Georg Lauterbach 2024-05-29 02:28:51 +02:00 committed by GitHub
parent 95d965fb76
commit f8b3f40276
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 40 additions and 36 deletions

View file

@ -53,8 +53,7 @@ function _log() {
return 1
fi
local LEVEL_AS_INT
local MESSAGE="${RESET}["
local LEVEL_AS_INT LOG_COLOR LOG_LEVEL_NAME MESSAGE
case "$(_get_log_level_or_default)" in
( 'trace' ) LEVEL_AS_INT=5 ;;
@ -67,27 +66,35 @@ function _log() {
case "${1}" in
( 'trace' )
[[ ${LEVEL_AS_INT} -ge 5 ]] || return 0
MESSAGE+=" ${CYAN}TRACE "
LOG_COLOR='CYAN'
LOG_LEVEL_NAME='TRACE'
;;
( 'debug' )
[[ ${LEVEL_AS_INT} -ge 4 ]] || return 0
MESSAGE+=" ${PURPLE}DEBUG "
LOG_COLOR='PURPLE'
LOG_LEVEL_NAME='DEBUG'
;;
( 'info' )
[[ ${LEVEL_AS_INT} -ge 3 ]] || return 0
MESSAGE+=" ${BLUE}INF "
LOG_COLOR='BLUE'
# the whitespace is intentional (for alignment purposes)
LOG_LEVEL_NAME='INFO '
;;
( 'warn' )
[[ ${LEVEL_AS_INT} -ge 2 ]] || return 0
MESSAGE+=" ${LYELLOW}WARNING "
LOG_COLOR='LYELLOW'
# the whitespace is intentional (for alignment purposes)
LOG_LEVEL_NAME='WARN '
;;
( 'error' )
[[ ${LEVEL_AS_INT} -ge 1 ]] || return 0
MESSAGE+=" ${LRED}ERROR " ;;
LOG_COLOR='LRED'
LOG_LEVEL_NAME='ERROR'
;;
( * )
_log 'error' "Call to '_log' with invalid log level argument '${1}'"
@ -95,7 +102,7 @@ function _log() {
;;
esac
MESSAGE+="${RESET}] ${2}"
MESSAGE="$(date --rfc-3339='seconds') ${!LOG_COLOR}${LOG_LEVEL_NAME}${RESET} $(basename "${0}"): ${2}"
if [[ ${1} =~ ^(warn|error)$ ]]; then
echo -e "${MESSAGE}" >&2
@ -104,11 +111,6 @@ function _log() {
fi
}
# 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,