Search code examples
c++spdlog

Spdlog: Only write to file if there is an error


At the moment I am combining a file sink with a console sink. (see bellow)

std::vector<spdlog::sink_ptr> sinks;
                    sinks.push_back(std::make_shared<spdlog::sinks::wincolor_stdout_sink_st>());
                    sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_st>(path, 23, 59));
                    std::shared_ptr<class spdlog::logger> logger = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));

I still want to log everything to the console, but only write to the file if there is an error. this is because at the moment there are a lot of useless logs and files.


Solution

  • You can call file_sink->set_level(spdlog::level::error) to make it log only on error and more severe levels.