// console auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); // Create a daily logger - a new file is created every day on 6:00am auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(log_file, 6, 00);
// 异步/同步模式 if (use_async) { spdlog::init_thread_pool(8192, thread_count); std::vector<spdlog::sink_ptr> log_sinks{console_sink, daily_sink}; auto logger = std::make_shared<spdlog::async_logger>("logger", log_sinks.begin(), log_sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); if (use_format) { logger->set_pattern(SPDLOG_LOG_PATTERN); } spdlog::set_default_logger(logger); } else { spdlog::sinks_init_list log_sinks = {console_sink, daily_sink}; auto logger = std::make_shared<spdlog::logger>("logger", log_sinks); if (use_format) { logger->set_pattern(SPDLOG_LOG_PATTERN); } spdlog::set_default_logger(logger); }
create_logger(log_file, level, use_async, show_use_level, use_format, flush_policy, thread_count); SPDLOG_TRACE("Trace from SpdLog!"); SPDLOG_DEBUG("Debug from SpdLog!"); SPDLOG_INFO("Info from SpdLog!"); SPDLOG_WARN("Warn from SpdLog!"); SPDLOG_ERROR("Error from SpdLog!"); SPDLOG_CRITICAL("Critical from SpdLog!"); }
1 2 3 4 5 6 7 8 9
test_thread_count [06/15 23:52:56.600533000] [ info ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/logger.h:77#create_logger] Info from SpdLog! [06/15 23:52:56.600539000] [ error ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/logger.h:79#create_logger] Error from SpdLog! [06/15 23:52:56.600566000] [warning ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/main.cpp:55#test_thread_count] Warn from SpdLog! [06/15 23:52:56.600540000] [critical] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/logger.h:80#create_logger] Critical from SpdLog! [06/15 23:52:56.600539000] [warning ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/logger.h:78#create_logger] Warn from SpdLog! [06/15 23:52:56.600566000] [ error ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/main.cpp:56#test_thread_count] Error from SpdLog! [06/15 23:52:56.600567000] [critical] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/main.cpp:57#test_thread_count] Critical from SpdLog! [06/15 23:52:56.600565000] [ info ] [ 8791/101640] [MyOTP/cpp-dev/tools/logger/main.cpp:54#test_thread_count] Info from SpdLog!