diff --git a/include/spdlog/cfg/log_levels.h b/include/spdlog/cfg/log_levels.h index ba3b2f75..3b8e6797 100644 --- a/include/spdlog/cfg/log_levels.h +++ b/include/spdlog/cfg/log_levels.h @@ -3,7 +3,7 @@ #pragma once -#include +#include #include #include @@ -17,30 +17,30 @@ class log_levels public: void set(const std::string &logger_name, level::level_enum lvl) { - if (logger_name.empty()) - { - default_level_ = lvl; - } - else - { - levels_[logger_name] = lvl; - } + levels_[logger_name] = lvl; } void set_default(level::level_enum lvl) { - default_level_ = lvl; + levels_[""] = lvl; } - level::level_enum get(const std::string &logger_name) + // configure log level of given logger if it appears in the config list or if default level is set + void update_logger_level(spdlog::logger &logger) { + auto &logger_name = logger.name(); auto it = levels_.find(logger_name); - return it != levels_.end() ? it->second : default_level_; - } - level::level_enum default_level() - { - return default_level_; + // if logger was not configured, check if default log level was configured + if (it == levels_.end()) + { + it = levels_.find(""); // + } + + if (it != levels_.end()) + { + logger.set_level(it->second); + } } }; } // namespace cfg diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 33835d80..0a25fb46 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -67,7 +67,8 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr new_logge new_logger->set_error_handler(err_handler_); } - new_logger->set_level(levels_.get(new_logger->name())); + levels_.update_logger_level(*new_logger); + new_logger->flush_on(flush_level_); if (backtrace_n_messages_ > 0) @@ -270,7 +271,7 @@ SPDLOG_INLINE void registry::update_levels(cfg::log_levels levels) for (auto &l : loggers_) { auto &logger = l.second; - logger->set_level(levels_.get(logger->name())); + levels_.update_logger_level(*logger); } } diff --git a/tests/test_cfg.cpp b/tests/test_cfg.cpp index 2e86210e..69499e1c 100644 --- a/tests/test_cfg.cpp +++ b/tests/test_cfg.cpp @@ -92,7 +92,7 @@ TEST_CASE("argv7", "[cfg]") spdlog::set_level(spdlog::level::info); } -TEST_CASE("level-not-found-test1", "[cfg]") +TEST_CASE("level-not-set-test1", "[cfg]") { spdlog::drop("l1"); const char *argv[] = {"ignore", ""}; @@ -103,10 +103,30 @@ TEST_CASE("level-not-found-test1", "[cfg]") REQUIRE(spdlog::default_logger()->level() == spdlog::level::info); } -TEST_CASE("level-not-found-test2", "[cfg]") +TEST_CASE("level-not-set-test2", "[cfg]") { spdlog::drop("l1"); + spdlog::drop("l2"); const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"}; + + auto l1 = spdlog::create("l1"); + l1->set_level(spdlog::level::warn); + auto l2 = spdlog::create("l2"); + l2->set_level(spdlog::level::warn); + + load_argv_levels(2, argv); + + REQUIRE(l1->level() == spdlog::level::trace); + REQUIRE(l2->level() == spdlog::level::warn); + REQUIRE(spdlog::default_logger()->level() == spdlog::level::info); +} + +TEST_CASE("level-not-set-test3", "[cfg]") +{ + spdlog::drop("l1"); + spdlog::drop("l2"); + const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"}; + load_argv_levels(2, argv); auto l1 = spdlog::create("l1"); @@ -116,3 +136,19 @@ TEST_CASE("level-not-found-test2", "[cfg]") REQUIRE(l2->level() == spdlog::level::info); REQUIRE(spdlog::default_logger()->level() == spdlog::level::info); } + +TEST_CASE("level-not-set-test4", "[cfg]") +{ + spdlog::drop("l1"); + spdlog::drop("l2"); + const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,warn"}; + + load_argv_levels(2, argv); + + auto l1 = spdlog::create("l1"); + auto l2 = spdlog::create("l2"); + + REQUIRE(l1->level() == spdlog::level::trace); + REQUIRE(l2->level() == spdlog::level::warn); + REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn); +}