From eaf070f4bca808054637e4ffed86b255ed547773 Mon Sep 17 00:00:00 2001 From: Graham Watts <34165628+GingerGraham@users.noreply.github.com> Date: Fri, 17 Apr 2026 17:53:55 +0100 Subject: [PATCH 1/2] feat: add init_message option to suppress initialization log entry Issues: #98 Discussion: #97 --- configuration/logging.conf.example | 6 ++++ demo-scripts/demo_config.sh | 40 ++++++++++++++++++++++++ docs/configuration.md | 3 ++ docs/initialization.md | 1 + logging.sh | 38 ++++++++++++++++++----- tests/test_initialization.sh | 49 ++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+), 8 deletions(-) diff --git a/configuration/logging.conf.example b/configuration/logging.conf.example index 219093d..8d209bc 100644 --- a/configuration/logging.conf.example +++ b/configuration/logging.conf.example @@ -69,6 +69,12 @@ stderr_level = ERROR # Set to true to suppress all console output (useful for daemon scripts) quiet = false +# Suppress the INIT message written to the log file on initialization (default: true) +# Set to false to prevent "Logger initialized by " from being appended to the log +# file on every init_logger call (useful when multiple scripts share the same log file, +# e.g. a main script and subscripts that all log to the same file) +init_message = true + # Enable verbose/debug logging (default: false) # When true, sets log level to DEBUG # Note: If both 'verbose' and 'level' are set, 'level' takes precedence diff --git a/demo-scripts/demo_config.sh b/demo-scripts/demo_config.sh index e4ffc2b..cf97737 100755 --- a/demo-scripts/demo_config.sh +++ b/demo-scripts/demo_config.sh @@ -108,6 +108,46 @@ log_error "Error message in JSON format" # Clean up temporary config file rm -f "$CONFIG_FILE" +echo -e "\n========== Testing init_message suppression ==========" +echo "When multiple scripts share a log file (e.g. a main script and subscripts)" +echo "repeated INIT entries can clutter the log. Use --no-init-message to suppress them." +echo + +SHARED_LOG="${LOGS_DIR}/demo_config_shared.log" +true > "$SHARED_LOG" + +echo "--- Run 1: default behaviour (INIT message written) ---" +init_logger --log "$SHARED_LOG" || { + echo "Failed to initialize logger" >&2 + exit 1 +} +log_info "Message from main script" + +echo "--- Run 2: subscript using --no-init-message (no extra INIT entry) ---" +init_logger --log "$SHARED_LOG" --no-init-message || { + echo "Failed to initialize logger" >&2 + exit 1 +} +log_info "Message from subscript" + +echo "--- Run 3: same suppression via config file ---" +cat > "$CONFIG_FILE" << 'EOF' +[logging] +init_message = false +EOF +init_logger --config "$CONFIG_FILE" --log "$SHARED_LOG" || { + echo "Failed to initialize logger" >&2 + exit 1 +} +log_info "Message from another subscript (config-driven)" + +echo +echo "Shared log file contents (note: only one INIT entry from run 1):" +cat "$SHARED_LOG" + +# Clean up +rm -f "$CONFIG_FILE" "$SHARED_LOG" + echo -e "\n========== Configuration File Demo Complete ==========" echo "Log file: ${LOGGING_FILE}" echo diff --git a/docs/configuration.md b/docs/configuration.md index 335ff7a..33a98eb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -8,6 +8,8 @@ configuration from code. * [Configuration File Format](#configuration-file-format) * [Configuration Keys](#configuration-keys) + * [Syslog Facility Key](#syslog-facility-key) + * [Configuration Value Limits](#configuration-value-limits) * [Boolean Values](#boolean-values) * [Key Aliases](#key-aliases) * [Using Configuration Files](#using-configuration-files) @@ -120,6 +122,7 @@ max_journal_length = 4096 | `unsafe_allow_ansi_codes` | `unsafe-allow-ansi-codes` | true/false, yes/no, on/off, 1/0 | `false` | Allow ANSI codes in log messages (unsafe) | | `max_line_length` | `max-line-length`, `log_max_line_length` | Non-negative integer (0 = unlimited) | `4096` | Max message length before formatting | | `max_journal_length` | `max-journal-length`, `journal_max_length` | Non-negative integer (0 = unlimited) | `4096` | Max message length for journal | +| `init_message` | `log_init_message` | true/false, yes/no, on/off, 1/0 | `true` | Write INIT entry to log file on startup | ### Syslog Facility Key diff --git a/docs/initialization.md b/docs/initialization.md index 1822fea..407fa0a 100644 --- a/docs/initialization.md +++ b/docs/initialization.md @@ -71,6 +71,7 @@ The `init_logger` function accepts the following options: | `-F, --facility FACILITY` | Set syslog facility for journal logs (default: daemon) | | `--color, --colour` | Explicitly enable color output (default: auto-detect) | | `--no-color, --no-colour` | Disable color output | +| `--no-init-message` | Suppress the INIT entry written to the log file on initialization | | `-U, --unsafe-allow-newlines` | Allow newlines in log messages (not recommended; disables sanitization) | | `-A, --unsafe-allow-ansi-codes` | Allow ANSI escape codes in log messages (not recommended; disables sanitization) | | `--max-line-length LENGTH` | Max message length before formatting for console/file output (0 = unlimited) | diff --git a/logging.sh b/logging.sh index c633f1a..4e043d3 100644 --- a/logging.sh +++ b/logging.sh @@ -87,6 +87,7 @@ LOG_FILE="" VERBOSE="false" CURRENT_LOG_LEVEL=$LOG_LEVEL_INFO USE_UTC="false" # Set to true to use UTC time in logs +LOG_INIT_MESSAGE="true" # Set to false to suppress the INIT message written to the log file on init_logger # Journal logging settings USE_JOURNAL="false" @@ -703,11 +704,25 @@ _parse_config_file() { echo " Hint: Using default value of 4096" >&2 fi ;; + init_message|log_init_message) + case "${value,,}" in + true|yes|1|on) + LOG_INIT_MESSAGE="true" + ;; + false|no|0|off) + LOG_INIT_MESSAGE="false" + ;; + *) + echo "Warning: Invalid init_message value '$value' at line $line_num, expected true/false" >&2 + ;; + esac + ;; *) echo "Warning: Unknown configuration key '$key' at line $line_num" >&2 echo " Hint: Valid keys are: level, format, log_file, journal, tag, utc, color," >&2 echo " stderr_level, quiet, console_log, script_name, verbose," >&2 - echo " unsafe_allow_newlines, unsafe_allow_ansi_codes, max_line_length, max_journal_length" >&2 + echo " unsafe_allow_newlines, unsafe_allow_ansi_codes, max_line_length, max_journal_length," >&2 + echo " init_message" >&2 ;; esac else @@ -1210,6 +1225,10 @@ init_logger() { fi shift 2 ;; + --no-init-message) + LOG_INIT_MESSAGE="false" + shift + ;; *) echo "Unknown parameter for logger: $1" >&2 return 1 @@ -1288,13 +1307,16 @@ init_logger() { fi # Write the initialization message using the same format - local init_message - init_message=$(_format_log_message "INIT" "Logger initialized by $SCRIPT_NAME") - echo "$init_message" >> "$LOG_FILE" 2>/dev/null || { - echo "Error: Failed to write test message to log file" >&2 - echo " Hint: Verify the file is writable and disk space is available" >&2 - return 1 - } + # Can be suppressed with --no-init-message (or init_message=false in config) + if [[ "$LOG_INIT_MESSAGE" == "true" ]]; then + local init_message + init_message=$(_format_log_message "INIT" "Logger initialized by $SCRIPT_NAME") + echo "$init_message" >> "$LOG_FILE" 2>/dev/null || { + echo "Error: Failed to write test message to log file" >&2 + echo " Hint: Verify the file is writable and disk space is available" >&2 + return 1 + } + fi echo "Logger: Successfully initialized with log file enabled" >&2 fi diff --git a/tests/test_initialization.sh b/tests/test_initialization.sh index f24e2e5..9a681d3 100755 --- a/tests/test_initialization.sh +++ b/tests/test_initialization.sh @@ -384,6 +384,52 @@ EOF pass_test } +# Test: INIT message written to log file by default +test_init_message_default() { + start_test "INIT message is written to log file by default" + + local log_file="$TEST_DIR/init_default.log" + init_logger --log "$log_file" + + assert_file_contains "$log_file" "INIT" || return + assert_file_contains "$log_file" "Logger initialized" || return + assert_equals "true" "$LOG_INIT_MESSAGE" || return + + pass_test +} + +# Test: --no-init-message suppresses the INIT log file entry +test_no_init_message_flag() { + start_test "--no-init-message suppresses INIT entry in log file" + + local log_file="$TEST_DIR/no_init_flag.log" + init_logger --log "$log_file" --no-init-message + + assert_file_not_contains "$log_file" "Logger initialized" || return + assert_equals "false" "$LOG_INIT_MESSAGE" || return + + pass_test +} + +# Test: init_message = false in config suppresses the INIT log file entry +test_no_init_message_config() { + start_test "init_message=false in config suppresses INIT entry in log file" + + local config_file="$TEST_DIR/no_init.conf" + cat > "$config_file" << 'EOF' +[logging] +init_message = false +EOF + + local log_file="$TEST_DIR/no_init_config.log" + init_logger --config "$config_file" --log "$log_file" + + assert_file_not_contains "$log_file" "Logger initialized" || return + assert_equals "false" "$LOG_INIT_MESSAGE" || return + + pass_test +} + # Run all tests test_default_initialization test_quiet_option @@ -413,3 +459,6 @@ test_script_name_short_option test_script_name_in_output test_script_name_from_config test_script_name_cli_overrides_config +test_init_message_default +test_no_init_message_flag +test_no_init_message_config From fef445d975a07b18dfc64c1a4d5382d4fd8e232f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Apr 2026 17:43:36 +0000 Subject: [PATCH 2/2] fix: address review feedback on init_message feature - Fix config comment wording: true writes INIT entry, false suppresses it - Add INIT level assertions to suppression tests for stronger coverage - Fix error message: 'test message' -> 'initialization message' Agent-Logs-Url: https://github.com/GingerGraham/bash-logger/sessions/1dca36e2-ad49-4b62-981a-169ed205db36 Co-authored-by: GingerGraham <34165628+GingerGraham@users.noreply.github.com> --- configuration/logging.conf.example | 8 ++++---- logging.sh | 2 +- tests/test_initialization.sh | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/configuration/logging.conf.example b/configuration/logging.conf.example index 8d209bc..da3b516 100644 --- a/configuration/logging.conf.example +++ b/configuration/logging.conf.example @@ -69,10 +69,10 @@ stderr_level = ERROR # Set to true to suppress all console output (useful for daemon scripts) quiet = false -# Suppress the INIT message written to the log file on initialization (default: true) -# Set to false to prevent "Logger initialized by " from being appended to the log -# file on every init_logger call (useful when multiple scripts share the same log file, -# e.g. a main script and subscripts that all log to the same file) +# Write the INIT message to the log file on initialization (default: true) +# Set to false to suppress "Logger initialized by " from being appended to the +# log file on every init_logger call (useful when multiple scripts share the same log +# file, e.g. a main script and subscripts that all log to the same file) init_message = true # Enable verbose/debug logging (default: false) diff --git a/logging.sh b/logging.sh index 4e043d3..0f876f0 100644 --- a/logging.sh +++ b/logging.sh @@ -1312,7 +1312,7 @@ init_logger() { local init_message init_message=$(_format_log_message "INIT" "Logger initialized by $SCRIPT_NAME") echo "$init_message" >> "$LOG_FILE" 2>/dev/null || { - echo "Error: Failed to write test message to log file" >&2 + echo "Error: Failed to write initialization message to log file" >&2 echo " Hint: Verify the file is writable and disk space is available" >&2 return 1 } diff --git a/tests/test_initialization.sh b/tests/test_initialization.sh index 9a681d3..740aa31 100755 --- a/tests/test_initialization.sh +++ b/tests/test_initialization.sh @@ -406,6 +406,7 @@ test_no_init_message_flag() { init_logger --log "$log_file" --no-init-message assert_file_not_contains "$log_file" "Logger initialized" || return + assert_file_not_contains "$log_file" " INIT " || return assert_equals "false" "$LOG_INIT_MESSAGE" || return pass_test @@ -425,6 +426,7 @@ EOF init_logger --config "$config_file" --log "$log_file" assert_file_not_contains "$log_file" "Logger initialized" || return + assert_file_not_contains "$log_file" " INIT " || return assert_equals "false" "$LOG_INIT_MESSAGE" || return pass_test