-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add init_message option to suppress initialization log entry #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 "Error: Failed to write test message to log file" >&2 | |
| echo "Error: Failed to write initialization message to log file" >&2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Comment on lines
+408
to
+410
|
||
|
|
||
| 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new config key is named
init_messageand the valuetruemeans the INIT message is written. The added comment block reads as if the setting suppresses the INIT message by default ("Suppress ... (default: true)"); please reword to reflect thattruewrites the INIT entry andfalsesuppresses it.