From 49001a24050aa95099353e3f14e8854b1196450f Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Sat, 20 Apr 2024 11:18:14 +0200 Subject: [PATCH] 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 --- lib/functions/log.sh | 17 +++++++++++------ test/run-super-linter-tests.sh | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/functions/log.sh b/lib/functions/log.sh index d50d6276..0de6290d 100755 --- a/lib/functions/log.sh +++ b/lib/functions/log.sh @@ -75,7 +75,7 @@ LOG_TEMP=$(mktemp) || echo "Failed to create temporary log file." export LOG_TEMP log() { - local PRINT_TO_STDOUT="${1}" + local EMIT_LOG_MESSAGE="${1}" local MESSAGE="${2}" local LOG_LEVEL_LABEL="${3}" @@ -107,12 +107,17 @@ log() { local MESSAGE_FOR_LOG_FILE MESSAGE_FOR_LOG_FILE="${LOG_MESSAGE_DATE} ${LOG_LEVEL_LABEL} ${MESSAGE}" - if [[ "${PRINT_TO_STDOUT}" == "true" ]]; then - echo -e "${COLORED_MESSAGE}" - fi + if [[ "${EMIT_LOG_MESSAGE}" == "true" ]]; then + # Emit colors only if there's a terminal + if [ -t 0 ]; then + echo -e "${COLORED_MESSAGE}" + else + echo -e "${MESSAGE_FOR_LOG_FILE}" + fi - if [ "${CREATE_LOG_FILE}" = "true" ]; then - echo -e "${MESSAGE_FOR_LOG_FILE}" >>"${LOG_TEMP}" + if [ "${CREATE_LOG_FILE}" = "true" ]; then + echo -e "${MESSAGE_FOR_LOG_FILE}" >>"${LOG_TEMP}" + fi fi } diff --git a/test/run-super-linter-tests.sh b/test/run-super-linter-tests.sh index 9e59d546..f13180d8 100755 --- a/test/run-super-linter-tests.sh +++ b/test/run-super-linter-tests.sh @@ -9,7 +9,7 @@ TEST_FUNCTION_NAME="${2}" 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() { 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_expect_success + CREATE_LOG_FILE="true" LOG_LEVEL="NOTICE" } @@ -81,6 +82,9 @@ run_test_case_git_initial_commit() { # Run the test setup function ${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 RUN_LOCAL="${RUN_LOCAL:-true}") 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}" +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 echo "Super-linter exited with an unexpected code: ${SUPER_LINTER_EXIT_CODE}" exit 1