Skip to content

How to create new loggers properly? #3446

@autoantwort

Description

@autoantwort

In my main method I create a custom logger with two sinks:

    std::vector<spdlog::sink_ptr> sinks;
    sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
    sinks.push_back(std::make_shared<my_sink<std::mutex>>(...));
    auto combined_logger = std::make_shared<spdlog::logger>("main", begin(sinks), end(sinks));
    spdlog::set_default_logger(combined_logger);
    spdlog::set_level(spdlog::level::debug);
    spdlog::cfg::load_env_levels();

Then in my classes I create loggers with names for the corresponding classes like so:

EventQueue::EventQueue()
    : logger(spdlog::default_logger()->clone("EventQueue"))
{
}

My expectation was when I set the env var SPDLOG_LEVEL="debug,EventQueue=info I expected that my EventQueue logger would have the info level, but the level is debug.
Must I call spdlog::apply_logger_env_levels(logger); every time for every logger I create? This seems wrong. How should I create the loggers so that the level from the env applies?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions