Fix #3525: Make level name matching case-insensitive (#3535)

Modified from_str() to perform case-insensitive comparison for all level names.
This allows environment variables and SPDLOG_LEVEL_NAMES to use uppercase or
mixed case level names (e.g., DEBUG, INFO, Warning) while maintaining full
backward compatibility with existing lowercase names.
This commit is contained in:
SamareshSingh
2026-02-09 13:58:20 -06:00
committed by GitHub
parent 6c5d63291a
commit 566b2d1404
3 changed files with 60 additions and 3 deletions

View File

@@ -176,3 +176,33 @@ TEST_CASE("restore-to-default", "[cfg]") {
load_argv_levels(2, argv);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
}
TEST_CASE("uppercase-level-names", "[cfg]") {
spdlog::drop("l1");
spdlog::drop("l2");
#ifdef CATCH_PLATFORM_WINDOWS
_putenv_s("SPDLOG_LEVEL", "l1=DEBUG,INFO");
#else
setenv("SPDLOG_LEVEL", "l1=DEBUG,INFO", 1);
#endif
load_env_levels();
auto l1 = spdlog::create<test_sink_st>("l1");
auto l2 = spdlog::create<test_sink_st>("l2");
REQUIRE(l1->level() == spdlog::level::debug);
REQUIRE(l2->level() == spdlog::level::info);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::info);
// Test with argv
spdlog::drop("l3");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l3=WARN,ERROR"};
load_argv_levels(2, argv);
auto l3 = spdlog::create<test_sink_st>("l3");
REQUIRE(l3->level() == spdlog::level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::err);
// Reset to info
spdlog::set_level(spdlog::level::info);
}