spdlog takes the “include what you need” approach - your code should includethe features that actually needed.
输出使用fmt库,类似python
编译器log过滤
1 2 3 4 5 6 7
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
spdlog::set_level(spdlog::level::debug); // or spdlog::set_level(spdlog::level::trace);
SPDLOG_LOGGER_TRACE(file_logger , "Some trace message that will not be evaluated.{} ,{}", 1, 3.23); SPDLOG_LOGGER_DEBUG(file_logger , "Some Debug message that will be evaluated.. {} ,{}", 1, 3.23); SPDLOG_DEBUG("Some debug message to default logger that will be evaluated");
set level
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
spdlog::set_level(spdlog::level::info); // Set global log level to info spdlog::debug("This message should not be displayed!"); spdlog::set_level(spdlog::level::trace); // Set specific logger's log level spdlog::debug("This message should be displayed.."); ```cpp
backtrace
```cpp // Backtrace support // Loggers can store in a ring buffer all messages (including debug/trace) for later inspection. // When needed, call dump_backtrace() to see what happened: spdlog::enable_backtrace(10); // create ring buffer with capacity of 10 messages for (int i = 0; i < 100; i++) { spdlog::debug("Backtrace message {}", i); // not logged.. } // e.g. if some error happened: spdlog::dump_backtrace(); // log them now!
shutdown
1 2 3
// Release all spdlog resources, and drop all loggers in the registry. // This is optional (only mandatory if using windows + async log). spdlog::shutdown();
auto sink = std::make_shared<spdlog::sinks::stdout_sink_mt>(); auto my_logger= std::make_shared<spdlog::logger>("mylogger", sink); // manually,不会register
std::vector<spdlog::sink_ptr> sinks; sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_st>()); sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_st>("logfile", 23, 59)); auto combined_logger = std::make_shared<spdlog::logger>("name", begin(sinks), end(sinks)); // one log and multiple sink //register it if you need to access it globally spdlog::register_logger(combined_logger);
spdlog maintains a global (per process) registry of the created loggers.
The purpose is for loggers to be accessed easily from anywhere in theproject without passing them around.(避免参数传递)
1 2 3 4 5
spdlog::get("logger_name"); // 内部有lock,最好使用一个变量保存 spdlog::register_logger(some_logger); spdlog::drop("logger_name"); //or remove them all spdlog::drop_all()