-
Notifications
You must be signed in to change notification settings - Fork 2k
config: fix startup abort when log_level set via env and config #12316
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
Open
saisree1217
wants to merge
2
commits into
fluent:master
Choose a base branch
from
saisree1217:fix-log-level-env-config-abort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||
|
|
||
| /* Fluent Bit | ||
| * ========== | ||
| * Copyright (C) 2015-2026 The Fluent Bit Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <fluent-bit/flb_info.h> | ||
| #include <fluent-bit/flb_config.h> | ||
| #include <fluent-bit/flb_log.h> | ||
|
|
||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #include "flb_tests_internal.h" | ||
|
|
||
| #define ENV_LOG_LEVEL "FLB_LOG_LEVEL" | ||
|
|
||
| #ifdef FLB_SYSTEM_WINDOWS | ||
| static int flb_test_setenv(const char *name, const char *value, int overwrite) | ||
| { | ||
| if (overwrite == 0 && getenv(name) != NULL) { | ||
| return 0; | ||
| } | ||
|
|
||
| return _putenv_s(name, value); | ||
| } | ||
|
|
||
| static int flb_test_unsetenv(const char *name) | ||
| { | ||
| return _putenv_s(name, ""); | ||
| } | ||
| #else | ||
| #define flb_test_setenv(name, value, overwrite) setenv(name, value, overwrite) | ||
| #define flb_test_unsetenv(name) unsetenv(name) | ||
| #endif | ||
|
|
||
| static char *save_env_log_level() | ||
| { | ||
| char *val; | ||
|
|
||
| val = getenv(ENV_LOG_LEVEL); | ||
| if (val != NULL && val[0] != '\0') { | ||
| return strdup(val); | ||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
| static void restore_env_log_level(char *saved) | ||
| { | ||
| if (saved != NULL) { | ||
| flb_test_setenv(ENV_LOG_LEVEL, saved, 1); | ||
| free(saved); | ||
| } | ||
| else { | ||
| flb_test_unsetenv(ENV_LOG_LEVEL); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Regression test for GitHub issue #12310: when FLB_LOG_LEVEL is set in the | ||
| * environment and log_level is also present in the [SERVICE] section, | ||
| * flb_config_set_property() must return success (the environment variable | ||
| * takes precedence). Before the fix it returned -1 on this path, which | ||
| * since 5.1.0 aborted startup. | ||
| */ | ||
| static void test_log_level_env_and_config() | ||
| { | ||
| int ret; | ||
| char *saved; | ||
| struct flb_config *config; | ||
|
|
||
| saved = save_env_log_level(); | ||
|
|
||
| ret = flb_test_setenv(ENV_LOG_LEVEL, "debug", 1); | ||
| TEST_CHECK(ret == 0); | ||
|
|
||
| config = flb_config_init(); | ||
| TEST_CHECK(config != NULL); | ||
| if (config == NULL) { | ||
| restore_env_log_level(saved); | ||
| return; | ||
| } | ||
|
|
||
| ret = flb_config_set_property(config, "log_level", "info"); | ||
| TEST_CHECK(ret == 0); | ||
| TEST_MSG("flb_config_set_property returned %d, expected 0", ret); | ||
|
|
||
| #ifndef FLB_HAVE_STATIC_CONF | ||
| /* the environment variable wins over the configuration value */ | ||
| TEST_CHECK(config->verbose == FLB_LOG_DEBUG); | ||
| TEST_MSG("verbose is %d, expected %d (debug)", | ||
| config->verbose, FLB_LOG_DEBUG); | ||
| #endif | ||
|
|
||
| flb_config_exit(config); | ||
| restore_env_log_level(saved); | ||
| } | ||
|
|
||
| static void test_log_level_config_only() | ||
| { | ||
| int ret; | ||
| char *saved; | ||
| struct flb_config *config; | ||
|
|
||
| saved = save_env_log_level(); | ||
| flb_test_unsetenv(ENV_LOG_LEVEL); | ||
|
|
||
| config = flb_config_init(); | ||
| TEST_CHECK(config != NULL); | ||
| if (config == NULL) { | ||
| restore_env_log_level(saved); | ||
| return; | ||
| } | ||
|
|
||
| ret = flb_config_set_property(config, "log_level", "info"); | ||
| TEST_CHECK(ret == 0); | ||
| TEST_CHECK(config->verbose == FLB_LOG_INFO); | ||
|
|
||
| /* an invalid value must still be reported as an error */ | ||
| ret = flb_config_set_property(config, "log_level", "invalid_level"); | ||
| TEST_CHECK(ret == -1); | ||
|
|
||
| flb_config_exit(config); | ||
| restore_env_log_level(saved); | ||
| } | ||
|
|
||
| TEST_LIST = { | ||
| { "log_level_env_and_config", test_log_level_env_and_config }, | ||
| { "log_level_config_only", test_log_level_config_only }, | ||
| { 0 } | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Preserve the initial empty-variable state.
Lines 54-58 treat a present but empty
FLB_LOG_LEVELas unset. Lines 63-69 then remove it after the test. On POSIX, an empty variable and an unset variable are different states.Record whether
getenv()returnedNULLseparately from its value. Restore an empty value when it was initially present. Use a platform-specific helper if the Windows CRT wrapper cannot represent that state.🤖 Prompt for AI Agents