Commit 3a68eecb authored by gabime's avatar gabime

Fix issue #1680

parent 54a8259b
......@@ -3,7 +3,7 @@
#pragma once
#include <spdlog/common.h>
#include <spdlog/logger.h>
#include <string>
#include <unordered_map>
......@@ -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
......
......@@ -67,7 +67,8 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> 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);
}
}
......
......@@ -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<spdlog::sinks::test_sink_st>("l1");
l1->set_level(spdlog::level::warn);
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("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<spdlog::sinks::test_sink_st>("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<spdlog::sinks::test_sink_st>("l1");
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
REQUIRE(l1->level() == spdlog::level::trace);
REQUIRE(l2->level() == spdlog::level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::level::warn);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment