fix: simplify log infrastructure (#5312)

- Move log variables in log.sh to shorten linter.sh
- Source log.sh as soon as possible, so we can use log functions and
  variables as soon as possible.
- Validate 'boolean' log variables: LOG_DEBUG, LOG_VERBOSE, LOG_NOTICE,
  LOG_WARN, LOG_ERROR.
- Move foreground color markers from an associative array (that we
  cannot export), inside the log function. This fixes an issue that we
  discovered while working on #5254 where color markers were not
  available to subprocesses.
- Remove background color markers because we don't use them.
This commit is contained in:
Marco Ferrari 2024-02-21 18:51:47 +01:00 committed by GitHub
parent 25003d0370
commit 0f91a56f21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 87 additions and 95 deletions

View file

@ -1,52 +1,74 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Background colors: # GitHub Actions variables to enable workflow debug logging
# Blue # Ref: https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging
# Cyan # Ref: https://github.com/actions/runner/pull/253
# Green declare -l ACTIONS_RUNNER_DEBUG
# Black ACTIONS_RUNNER_DEBUG="${ACTIONS_RUNNER_DEBUG:-"false"}"
# Magenta declare -l ACTIONS_STEPS_DEBUG
# Red ACTIONS_STEPS_DEBUG="${ACTIONS_STEPS_DEBUG:-"false"}"
# White declare -i RUNNER_DEBUG
# Yellow RUNNER_DEBUG="${RUNNER_DEBUG:-0}"
declare -Agr B=(
[B]=$(echo -e "\e[44m")
[C]=$(echo -e "\e[46m")
[G]=$(echo -e "\e[42m")
[K]=$(echo -e "\e[40m")
[M]=$(echo -e "\e[45m")
[R]=$(echo -e "\e[41m")
[W]=$(echo -e "\e[47m")
[Y]=$(echo -e "\e[43m")
)
# Foreground colors: # Default log file name (located in GITHUB_WORKSPACE folder)
# Blue LOG_FILE="${LOG_FILE:-"super-linter.log"}"
# Cyan LOG_LEVEL="${LOG_LEVEL:-"INFO"}"
# Green declare -l CREATE_LOG_FILE
# Black CREATE_LOG_FILE="${CREATE_LOG_FILE:-"false"}"
# Magenta
# Red
# White
# Yellow
declare -Agr F=(
[B]=$(echo -e "\e[0;34m")
[C]=$(echo -e "\e[0;36m")
[G]=$(echo -e "\e[0;32m")
[K]=$(echo -e "\e[0;30m")
[M]=$(echo -e "\e[0;35m")
[R]=$(echo -e "\e[0;31m")
[W]=$(echo -e "\e[0;37m")
[Y]=$(echo -e "\e[0;33m")
)
# Reset if [[ ${ACTIONS_RUNNER_DEBUG} == true ]] ||
NC=$(echo -e "\e[0m") [[ ${ACTIONS_STEPS_DEBUG} == true ]] ||
readonly NC [[ ${RUNNER_DEBUG} -eq 1 ]]; then
LOG_LEVEL="DEBUG"
echo "ACTIONS_RUNNER_DEBUG: ${ACTIONS_RUNNER_DEBUG}. ACTIONS_STEPS_DEBUG: ${ACTIONS_STEPS_DEBUG}. RUNNER_DEBUG: ${RUNNER_DEBUG}. Setting LOG_LEVEL to: ${LOG_LEVEL}"
fi
export B declare -l LOG_DEBUG
export F LOG_DEBUG="false"
export NC declare -l LOG_ERROR
LOG_VERBOSE="false"
declare -l LOG_ERROR
LOG_NOTICE="false"
declare -l LOG_ERROR
LOG_WARN="false"
declare -l LOG_ERROR
LOG_ERROR="false"
if [[ ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then
LOG_DEBUG="true"
LOG_VERBOSE="true"
LOG_NOTICE="true"
LOG_WARN="true"
LOG_ERROR="true"
fi
if [[ ${LOG_LEVEL} == "INFO" || ${LOG_LEVEL} == "VERBOSE" ]]; then
LOG_VERBOSE="true"
LOG_NOTICE="true"
LOG_WARN="true"
LOG_ERROR="true"
fi
if [[ ${LOG_LEVEL} == "NOTICE" ]]; then
LOG_NOTICE="true"
LOG_WARN="true"
LOG_ERROR="true"
fi
if [[ ${LOG_LEVEL} == "WARN" ]]; then
LOG_WARN="true"
LOG_ERROR="true"
fi
if [[ ${LOG_LEVEL} == "ERROR" ]]; then
LOG_ERROR="true"
fi
export LOG_DEBUG
export LOG_VERBOSE
export LOG_NOTICE
export LOG_WARN
export LOG_ERROR
LOG_TEMP=$(mktemp) || echo "Failed to create temporary log file." LOG_TEMP=$(mktemp) || echo "Failed to create temporary log file."
export LOG_TEMP export LOG_TEMP
@ -59,14 +81,22 @@ log() {
local LOG_MESSAGE_DATE local LOG_MESSAGE_DATE
LOG_MESSAGE_DATE="$(date +"%F %T")" LOG_MESSAGE_DATE="$(date +"%F %T")"
local COLOR_MARKER local COLOR_MARKER
COLOR_MARKER="${F[B]}" # Set foreground color to blue
COLOR_MARKER=$(echo -e "\e[0;34m")
# Reset colors
local NC
NC=$(echo -e "\e[0m")
if [ "${LOG_LEVEL_LABEL}" == "NOTICE" ]; then if [ "${LOG_LEVEL_LABEL}" == "NOTICE" ]; then
COLOR_MARKER="${F[G]}" # Set foreground color to green
COLOR_MARKER=$(echo -e "\e[0;32m")
elif [ "${LOG_LEVEL_LABEL}" == "WARN" ]; then elif [ "${LOG_LEVEL_LABEL}" == "WARN" ]; then
COLOR_MARKER="${F[Y]}" # Set foreground color to yellow
COLOR_MARKER=$(echo -e "\e[0;33m")
elif [ "${LOG_LEVEL_LABEL}" == "ERROR" ] || [ "${LOG_LEVEL_LABEL}" == "FATAL" ]; then elif [ "${LOG_LEVEL_LABEL}" == "ERROR" ] || [ "${LOG_LEVEL_LABEL}" == "FATAL" ]; then
COLOR_MARKER="${F[R]}" # Set foreground color to red
COLOR_MARKER=$(echo -e "\e[0;31m")
fi fi
LOG_LEVEL_LABEL="[${LOG_LEVEL_LABEL}]" LOG_LEVEL_LABEL="[${LOG_LEVEL_LABEL}]"

View file

@ -7,6 +7,11 @@ function ValidateBooleanConfigurationVariables() {
ValidateBooleanVariable "ENABLE_GITHUB_ACTIONS_GROUP_TITLE" "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}" ValidateBooleanVariable "ENABLE_GITHUB_ACTIONS_GROUP_TITLE" "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}"
ValidateBooleanVariable "IGNORE_GENERATED_FILES" "${IGNORE_GENERATED_FILES}" ValidateBooleanVariable "IGNORE_GENERATED_FILES" "${IGNORE_GENERATED_FILES}"
ValidateBooleanVariable "IGNORE_GITIGNORED_FILES" "${IGNORE_GITIGNORED_FILES}" ValidateBooleanVariable "IGNORE_GITIGNORED_FILES" "${IGNORE_GITIGNORED_FILES}"
ValidateBooleanVariable "LOG_DEBUG" "${LOG_DEBUG}"
ValidateBooleanVariable "LOG_ERROR" "${LOG_ERROR}"
ValidateBooleanVariable "LOG_NOTICE" "${LOG_NOTICE}"
ValidateBooleanVariable "LOG_VERBOSE" "${LOG_VERBOSE}"
ValidateBooleanVariable "LOG_WARN" "${LOG_WARN}"
ValidateBooleanVariable "MULTI_STATUS" "${MULTI_STATUS}" ValidateBooleanVariable "MULTI_STATUS" "${MULTI_STATUS}"
ValidateBooleanVariable "RUN_LOCAL" "${RUN_LOCAL}" ValidateBooleanVariable "RUN_LOCAL" "${RUN_LOCAL}"
ValidateBooleanVariable "SSH_INSECURE_NO_VERIFY_GITHUB_KEY" "${SSH_INSECURE_NO_VERIFY_GITHUB_KEY}" ValidateBooleanVariable "SSH_INSECURE_NO_VERIFY_GITHUB_KEY" "${SSH_INSECURE_NO_VERIFY_GITHUB_KEY}"

View file

@ -4,57 +4,16 @@ set -o errexit
set -o nounset set -o nounset
set -o pipefail set -o pipefail
##################################################################
# Debug Vars #
# Define these early, so we can use debug logging ASAP if needed #
##################################################################
# GitHub Actions variables to enable workflow debug logging
# Ref: https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging
# Ref: https://github.com/actions/runner/pull/253
declare -l ACTIONS_RUNNER_DEBUG
ACTIONS_RUNNER_DEBUG="${ACTIONS_RUNNER_DEBUG:-"false"}"
declare -l ACTIONS_STEPS_DEBUG
ACTIONS_STEPS_DEBUG="${ACTIONS_STEPS_DEBUG:-"false"}"
declare -i RUNNER_DEBUG
RUNNER_DEBUG="${RUNNER_DEBUG:-0}"
# Version of the Super-linter (standard,slim,etc) # Version of the Super-linter (standard,slim,etc)
IMAGE="${IMAGE:-standard}" IMAGE="${IMAGE:-standard}"
##################################################################
# Log Vars #
# Define these early, so we can use debug logging ASAP if needed #
##################################################################
LOG_FILE="${LOG_FILE:-"super-linter.log"}" # Default log file name (located in GITHUB_WORKSPACE folder)
LOG_LEVEL="${LOG_LEVEL:-"INFO"}"
declare -l CREATE_LOG_FILE
CREATE_LOG_FILE="${CREATE_LOG_FILE:-"false"}"
if [[ ${ACTIONS_RUNNER_DEBUG} == true ]] ||
[[ ${ACTIONS_STEPS_DEBUG} == true ]] ||
[[ ${RUNNER_DEBUG} -eq 1 ]]; then
LOG_LEVEL="DEBUG"
echo "ACTIONS_RUNNER_DEBUG: ${ACTIONS_RUNNER_DEBUG}. ACTIONS_STEPS_DEBUG: ${ACTIONS_STEPS_DEBUG}. RUNNER_DEBUG: ${RUNNER_DEBUG}. Setting LOG_LEVEL to: ${LOG_LEVEL}"
fi
# Boolean to see debug logs
LOG_DEBUG=$(if [[ ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi)
export LOG_DEBUG
# Boolean to see info logs
LOG_VERBOSE=$(if [[ ${LOG_LEVEL} == "INFO" || ${LOG_LEVEL} == "VERBOSE" || ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi)
export LOG_VERBOSE
# Boolean to see notice logs
LOG_NOTICE=$(if [[ ${LOG_LEVEL} == "NOTICE" || ${LOG_LEVEL} == "INFO" || ${LOG_LEVEL} == "VERBOSE" || ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi)
export LOG_NOTICE
# Boolean to see warn logs
LOG_WARN=$(if [[ ${LOG_LEVEL} == "WARN" || ${LOG_LEVEL} == "NOTICE" || ${LOG_LEVEL} == "INFO" || ${LOG_LEVEL} == "VERBOSE" || ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi)
export LOG_WARN
# Boolean to see error logs
LOG_ERROR=$(if [[ ${LOG_LEVEL} == "ERROR" || ${LOG_LEVEL} == "WARN" || ${LOG_LEVEL} == "NOTICE" || ${LOG_LEVEL} == "INFO" || ${LOG_LEVEL} == "VERBOSE" || ${LOG_LEVEL} == "DEBUG" || ${LOG_LEVEL} == "TRACE" ]]; then echo "true"; fi)
export LOG_ERROR
######################### #########################
# Source Function Files # # Source Function Files #
######################### #########################
# Source log functions and variables early so we can use them ASAP
# shellcheck source=/dev/null
source /action/lib/functions/log.sh # Source the function script(s)
# shellcheck source=/dev/null # shellcheck source=/dev/null
source /action/lib/functions/buildFileList.sh # Source the function script(s) source /action/lib/functions/buildFileList.sh # Source the function script(s)
# shellcheck source=/dev/null # shellcheck source=/dev/null
@ -62,8 +21,6 @@ source /action/lib/functions/detectFiles.sh # Source the function script(s)
# shellcheck source=/dev/null # shellcheck source=/dev/null
source /action/lib/functions/linterRules.sh # Source the function script(s) source /action/lib/functions/linterRules.sh # Source the function script(s)
# shellcheck source=/dev/null # shellcheck source=/dev/null
source /action/lib/functions/log.sh # Source the function script(s)
# shellcheck source=/dev/null
source /action/lib/functions/updateSSL.sh # Source the function script(s) source /action/lib/functions/updateSSL.sh # Source the function script(s)
# shellcheck source=/dev/null # shellcheck source=/dev/null
source /action/lib/functions/validation.sh # Source the function script(s) source /action/lib/functions/validation.sh # Source the function script(s)