fix: respect log level when writing to the log (#5546)

- Write log messages in the log file according to the LOG_LEVEL that the
  user configured (or the default), instead of printing all the messages
  regardless of LOG_LEVEL to the log file.
- Don't emit colors if there is no terminal

Close #5337
This commit is contained in:
Marco Ferrari 2024-04-20 11:18:14 +02:00 committed by GitHub
parent 2baa96f9a9
commit 49001a2405
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View file

@ -75,7 +75,7 @@ LOG_TEMP=$(mktemp) || echo "Failed to create temporary log file."
export LOG_TEMP export LOG_TEMP
log() { log() {
local PRINT_TO_STDOUT="${1}" local EMIT_LOG_MESSAGE="${1}"
local MESSAGE="${2}" local MESSAGE="${2}"
local LOG_LEVEL_LABEL="${3}" local LOG_LEVEL_LABEL="${3}"
@ -107,13 +107,18 @@ log() {
local MESSAGE_FOR_LOG_FILE local MESSAGE_FOR_LOG_FILE
MESSAGE_FOR_LOG_FILE="${LOG_MESSAGE_DATE} ${LOG_LEVEL_LABEL} ${MESSAGE}" MESSAGE_FOR_LOG_FILE="${LOG_MESSAGE_DATE} ${LOG_LEVEL_LABEL} ${MESSAGE}"
if [[ "${PRINT_TO_STDOUT}" == "true" ]]; then if [[ "${EMIT_LOG_MESSAGE}" == "true" ]]; then
# Emit colors only if there's a terminal
if [ -t 0 ]; then
echo -e "${COLORED_MESSAGE}" echo -e "${COLORED_MESSAGE}"
else
echo -e "${MESSAGE_FOR_LOG_FILE}"
fi fi
if [ "${CREATE_LOG_FILE}" = "true" ]; then if [ "${CREATE_LOG_FILE}" = "true" ]; then
echo -e "${MESSAGE_FOR_LOG_FILE}" >>"${LOG_TEMP}" echo -e "${MESSAGE_FOR_LOG_FILE}" >>"${LOG_TEMP}"
fi fi
fi
} }
debug() { log "${LOG_DEBUG}" "$*" "DEBUG"; } debug() { log "${LOG_DEBUG}" "$*" "DEBUG"; }

View file

@ -9,7 +9,7 @@ TEST_FUNCTION_NAME="${2}"
DEFAULT_BRANCH="main" DEFAULT_BRANCH="main"
COMMAND_TO_RUN=(docker run -e DEFAULT_BRANCH="${DEFAULT_BRANCH}" -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true) COMMAND_TO_RUN=(docker run -t -e DEFAULT_BRANCH="${DEFAULT_BRANCH}" -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE=true)
configure_linters_for_test_cases() { configure_linters_for_test_cases() {
COMMAND_TO_RUN+=(-e TEST_CASE_RUN=true -e JSCPD_CONFIG_FILE=".jscpd-test-linters.json" -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" -e TYPESCRIPT_STANDARD_TSCONFIG_FILE=".github/linters/tsconfig.json") COMMAND_TO_RUN+=(-e TEST_CASE_RUN=true -e JSCPD_CONFIG_FILE=".jscpd-test-linters.json" -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" -e TYPESCRIPT_STANDARD_TSCONFIG_FILE=".github/linters/tsconfig.json")
@ -28,6 +28,7 @@ run_test_cases_expect_success() {
run_test_cases_log_level() { run_test_cases_log_level() {
run_test_cases_expect_success run_test_cases_expect_success
CREATE_LOG_FILE="true"
LOG_LEVEL="NOTICE" LOG_LEVEL="NOTICE"
} }
@ -81,6 +82,9 @@ run_test_case_git_initial_commit() {
# Run the test setup function # Run the test setup function
${TEST_FUNCTION_NAME} ${TEST_FUNCTION_NAME}
CREATE_LOG_FILE="${CREATE_LOG_FILE:-false}"
COMMAND_TO_RUN+=(-e CREATE_LOG_FILE="${CREATE_LOG_FILE}")
COMMAND_TO_RUN+=(-e LOG_LEVEL="${LOG_LEVEL:-"DEBUG"}") COMMAND_TO_RUN+=(-e LOG_LEVEL="${LOG_LEVEL:-"DEBUG"}")
COMMAND_TO_RUN+=(-e RUN_LOCAL="${RUN_LOCAL:-true}") COMMAND_TO_RUN+=(-e RUN_LOCAL="${RUN_LOCAL:-true}")
COMMAND_TO_RUN+=(-v "${SUPER_LINTER_WORKSPACE:-$(pwd)}:/tmp/lint") COMMAND_TO_RUN+=(-v "${SUPER_LINTER_WORKSPACE:-$(pwd)}:/tmp/lint")
@ -103,6 +107,20 @@ set -o errexit
echo "Super-linter exit code: ${SUPER_LINTER_EXIT_CODE}" echo "Super-linter exit code: ${SUPER_LINTER_EXIT_CODE}"
if [[ "${CREATE_LOG_FILE}" == true ]]; then
LOG_FILE_PATH="$(pwd)/super-linter.log"
if [ ! -e "${LOG_FILE_PATH}" ]; then
echo "Log file was requested but it's not available"
exit 1
else
sudo chown -R "$(id -u)":"$(id -g)" "${LOG_FILE_PATH}"
echo "Log file contents:"
cat "${LOG_FILE_PATH}"
fi
else
echo "Log file was not requested. CREATE_LOG_FILE: ${CREATE_LOG_FILE}"
fi
if [ ${SUPER_LINTER_EXIT_CODE} -ne ${EXPECTED_EXIT_CODE} ]; then if [ ${SUPER_LINTER_EXIT_CODE} -ne ${EXPECTED_EXIT_CODE} ]; then
echo "Super-linter exited with an unexpected code: ${SUPER_LINTER_EXIT_CODE}" echo "Super-linter exited with an unexpected code: ${SUPER_LINTER_EXIT_CODE}"
exit 1 exit 1