#include<iostream> #include"spdlog/spdlog.h" #include"spdlog/sinks/basic_file_sink.h"// support for basic file logging #include"spdlog/sinks/rotating_file_sink.h"// support for rotating file logging
#include<iostream> #include"spdlog/spdlog.h" #include"spdlog/async.h"//support for async logging. #include"spdlog/sinks/basic_file_sink.h"
intmain(int, char* []) { try { auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt"); for (int i = 1; i < 101; ++i) { async_file->info("Async message #{}", i); } // Under VisualStudio, this must be called before main finishes to workaround a known VS issue spdlog::drop_all(); } catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; } }
创建异步记录器并更改线程池设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include"spdlog/async.h"//support for async logging #include"spdlog/sinks/daily_file_sink.h" intmain(int, char* []) { try { auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("logfile", 23, 59); // default thread pool settings can be modified *before* creating the async logger: spdlog::init_thread_pool(10000, 1); // queue with 10K items and 1 backing thread. auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt"); spdlog::drop_all(); } catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; } }
// // Logger with console and file output. // the console will show only warnings or worse, while the file will log all messages. // #include<iostream> #include"spdlog/spdlog.h" #include"spdlog/sinks/stdout_color_sinks.h"// or "../stdout_sinks.h" if no colors needed #include"spdlog/sinks/basic_file_sink.h" intmain(int, char* []) { try { auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); console_sink->set_level(spdlog::level::warn); console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true); file_sink->set_level(spdlog::level::trace);
spdlog::logger logger("multi_sink", sink_list.begin(), sink_list.end()); logger.set_level(spdlog::level::debug); logger.warn("this should appear in both console and file"); logger.info("this message should not appear in the console, only in the file");
// or you can even set multi_sink logger as default logger spdlog::set_default_logger(std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list({console_sink, file_sink})));
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");
voidcustom_class_example() { some_class c; auto console = spdlog::stdout_logger_mt("console"); console->info("custom class with operator<<: {}..", c); }
voidcustom_class_example() { some_class c; c.code = 17; auto console = spdlog::stdout_logger_mt("console"); console->info("custom class with operator<< using fmt: {}..", c); }