Head-only库
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE //为了使用源码函数行号等符号用的
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"// 这个是异步使用log
void multi_sink_example2()
{spdlog::init_thread_pool(8192, 1);spdlog::set_level(spdlog::level::trace);//consoleauto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();stdout_sink->set_level(spdlog::level::trace);// stdout_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] [thread %t] %v");stdout_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [thread %t] %v");//file,rotating// auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("mylog.txt", 1024*1024*10,3);// rotating_sink->set_level(spdlog::level::debug);// rotating_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [thread %t] [%g:%#] [Fcn:%!] %v");//file,dailyauto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("log", 23, 59);daily_sink->set_level(spdlog::level::trace);daily_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [thread %t] [%g:%#] [Fcn:%!] %v");std::vector<spdlog::sink_ptr> sinks {stdout_sink, daily_sink};//create loggerauto logger = std::make_shared<spdlog::async_logger>("logger", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);spdlog::register_logger(logger);// spdlog::set_default_logger(logger);logger->info("logger-info-multi_sink_example2");logger->debug("logger-debug-multi_sink_example2");logger->error("logger-error-multi_sink_example2");logger->trace("logger-trace-multi_sink_example2");logger->warn("logger-warn-multi_sink_example2");logger->critical("logger-critical-multi_sink_example2");// logger->trace("multi_sink_example2");SPDLOG_LOGGER_TRACE(logger,"multi_sink_example2-trace");SPDLOG_LOGGER_INFO(logger,"multi_sink_example2-info");SPDLOG_LOGGER_WARN(logger, "warn message");SPDLOG_LOGGER_ERROR(logger, "error message");SPDLOG_LOGGER_CRITICAL(logger, "critical message");SPDLOG_LOGGER_DEBUG(logger, "debug message");
}
int main()
{#if 0//这是默认的log方法,不是异步的spdlog::info("Welcome to spdlog!");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: {0:b}", 42);spdlog::info("Support for floats {:03.2f}", 1.23456);spdlog::info("Positional args are {1} {0}..", "too", "supported");spdlog::info("{:<30}", "left aligned");spdlog::debug("This message should be displayed.."); spdlog::set_level(spdlog::level::trace); // Set global log level to debugspdlog::debug("This message should be displayed..");spdlog::trace("Trace:This message should be displayed.."); // change log pattern// spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");// spdlog::debug("change log pattern.."); // Compile time log levels// Note that this does not change the current log level, it will only// remove (depending on SPDLOG_ACTIVE_LEVEL) the call on the release code.SPDLOG_TRACE("Some trace message with param {}", 42);SPDLOG_DEBUG("Some debug message");
#endifmulti_sink_example2();}/*
If you need to use source location flags like %s, %g, %#, %!, is necessary to define this compiler flag:#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
(change the log level according to your needs) and use these macros:SPDLOG_LOGGER_TRACE(some_logger, "trace message");
SPDLOG_LOGGER_DEBUG(some_logger, "debug message");
SPDLOG_LOGGER_INFO(some_logger, "info message");
SPDLOG_LOGGER_WARN(some_logger, "warn message");
SPDLOG_LOGGER_ERROR(some_logger, "error message");
SPDLOG_LOGGER_CRITICAL(some_logger, "critical message");NOTE: if you've set the flag to SPDLOG_LEVEL_TRACE but you don't see any trace and/or debug messages, it's most likely something like this https://github.com/gabime/spdlog/issues/2764. In that case define it directly as a compiler flag. For example, using cmake:add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)Set global patten
Format can be applied globally to all registered loggers:spdlog::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");
*/