// https://blog.csdn.net/tutou_gou/article/details/121284474#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"spdlog/sinks/daily_file_sink.h"#include"spdlog/async.h"intmain(){try{//在logs/basic.txt中写下Hello worldauto my_logger = spdlog::basic_logger_mt("sbasic_logger","../logs/basic.txt");
my_logger->info("Hello {}","world");}catch(const spdlog::spdlog_ex& ex){
std::cout <<"Log initialization failed: "<< ex.what()<< std::endl;}try{auto file_logger = spdlog::rotating_logger_mt("file_logger","../logs/myfilename",1024*1024*5,10);
file_logger->set_level(spdlog::level::debug);int i =0;while(i <1000000){
file_logger->debug("Async message #{}", i);
i++;}}catch(const spdlog::spdlog_ex& ex){
std::cout <<"Log initialization failed: "<< ex.what()<< std::endl;}try{// Create a file rotating logger with 5mb size max and 3 rotated filesauto max_size =1048576*5;auto max_files =3;auto logger = spdlog::rotating_logger_mt("some_logger_name","../logs/rotating.txt", max_size, max_files);
logger->set_level(spdlog::level::debug);int i =0;while(i <1000000){
logger->debug("Async message #{}", i);
i++;}}catch(const spdlog::spdlog_ex& ex){
std::cout <<"Log initialization failed: "<< ex.what()<< std::endl;}// Create a daily logger - a new file is created every day on 2:30amauto daily_logger = spdlog::daily_logger_mt("daily_logger","../logs/daily.txt",11,16);// trigger flush if the log severity is error or higher
daily_logger->flush_on(spdlog::level::err);
daily_logger->info(123.44);
spdlog::drop_all();try{
spdlog::init_thread_pool(10000,1);auto file_logger = spdlog::rotating_logger_mt<spdlog::async_factory>("file_logger","../logs/mylogs.txt",1024*1024*5,4);int i =0;
file_logger->set_level(spdlog::level::debug);while(i <1000000){
file_logger->debug("Async message #{}", i);
i++;}}catch(const spdlog::spdlog_ex& ex){
std::cout <<"Log initialization failed: "<< ex.what()<< std::endl;}}
example.cpp
//直接包含头文件即可使用//Basic usage#include<spdlog/spdlog.h>//Create stdout/stderr logger object#include"spdlog/sinks/stdout_color_sinks.h"voidstdout_example(){// create color multi threaded loggerauto console = spdlog::stdout_color_mt("console");auto err_logger = spdlog::stderr_color_mt("stderr");
spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
spdlog::get("stderr")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name_std_err)");}intmain(){
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::error("Some error message with arg:{}",1);
spdlog::warn("Easy padding in numbers like {:08d}",12);
spdlog::critical("Support for int :{0:d};hex{0:x};oct:{0:o};bin:{o:b}",42);
spdlog::info("Support for floats {:00.2f}",1.23456);
spdlog::info("Positional args are {1} {0}..","too","support");
spdlog::info("{:<30}","left aligned");
spdlog::set_level(spdlog::level::debug);// Set global log level to debug
spdlog::debug("This message should be displayed..");//change log pattern
spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L----%$] [thread %t] %v");
spdlog::info("---------------------------");
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
spdlog::error("Some error message with arg:{}",1);
spdlog::warn("Easy padding in numbers like {:08d}",12);
spdlog::critical("Support for int :{0:d};hex{0:x};oct:{0:o};bin:{o:b}",42);
spdlog::info("Support for floats {:00.2f}",1.23456);
spdlog::info("Positional args are {1} {0}..","too","support");
spdlog::info("{:<30}","left aligned");// Compile time log levels// define SPDLOG_ACTIVE_LEVEL to desired level
spdlog::info("---------------------------");SPDLOG_TRACE("Some trace message with param {}",42);SPDLOG_DEBUG("Some debug message");stdout_example();return0;}