安装
头文件安装
静态库
$ git clone https://github.com/gabime/spdlog.git
$ cd spdlog && mkdir build && cd build
$ cmake .. && make -j
level
enum level_enum
{
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
warn = SPDLOG_LEVEL_WARN,
err = SPDLOG_LEVEL_ERROR,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
n_levels
};
stdout
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
void stdout_example()
{
// create color multi threaded logger
auto 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)");
}
file
basic file logger
常规txt文件
#include "spdlog/sinks/basic_file_sink.h" // support for basic file logging
auto my_logger = spdlog::basic_logger_mt("basic_logger", "logs/basic.txt");
rotating logger
日志轮转,指定日志数量。创建新日志时,会自动命名老日志,超过指定数目的老日志会被删除。
#include "spdlog/sinks/rotating_file_sink.h" // support for rotating file logging
auto file_logger = spdlog::rotating_logger_mt("file_logger", "myfilename", 1024 * 1024 * 5, 3);
daily file
#include "spdlog/sinks/daily_file_sink.h"
void daily_example()
{
// Create a daily logger - a new file is created every day on 2:30am.
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
}
asynchronous logger
异步日志,用于多线程。
#include "spdlog/async.h" //support for async logging.
#include "spdlog/sinks/basic_file_sink.h"
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
Create a multiple loggers all sharing the same file (sink) aka categories
共用一个输出文件
auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("logfile", 23, 59);
// create synchronous loggers
auto net_logger = std::make_shared<spdlog::logger>("net", daily_sink);
auto hw_logger = std::make_shared<spdlog::logger>("hw", daily_sink);
auto db_logger = std::make_shared<spdlog::logger>("db", daily_sink);
net_logger->set_level(spdlog::level::critical); // independent levels
hw_logger->set_level(spdlog::level::debug);
// globally register the loggers so the can be accessed using spdlog::get(logger_name)
spdlog::register_logger(net_logger);
Create a logger with multiple sinks, each sink with its own formatting and log level
控制多个输出
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::sinks_init_list sink_list = { file_sink, console_sink };
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})));
多线程
线程不安全
set_error_handler(log_err_handler);
logger::sinks() - returns a reference to a non thread safe vector, so don't modify it concurrently (e.g. logger->sinks().push_back(new_sink);)
线程标志
- _mt :多线程
auto logger = spdlog::basic_logger_mt(...);
- _st: 单线程
auto logger = spdlog::basic_logger_st(...);
格式
格式字符串
set_pattern(pattern_string);
全局
spdlog::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***");
指定logger对象
some_logger->set_pattern(">>>>>>>>> %H:%M:%S %z %v <<<<<<<<<");
指定sink对象
some_logger->sinks()[0]->set_pattern(">>>>>>>>> %H:%M:%S %z %v <<<<<<<<<");
some_logger->sinks()[1]->set_pattern("..");
实战
mian中声明,多线程调用其他文件中定义的类。
- 只要在项目的某一处自动声明了logger,就可在项目的其他地方通过get调用该logger。 故可采用在main中进行声明,在其他文件或类中进行调用。
- 自动声明的logger为:basic file logger,rotating logger,asynchronous logger, daily file logger;
- 如无async_log.txt会自动创建;如有,会在末尾追加。
- 如采用了非自动声明的logger,需手动声明。以官方的Create a multiple loggers all sharing the same file (sink) aka categories进行说明
- 非自动声明的logger为:std::make_shared声明的。
#include <iostream>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
int main(int, char* [])
{
try
{
auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("logfile", 23, 59);
// create synchronous loggers
// 非自动声明的logger
auto net_logger = std::make_shared<spdlog::logger>("net", daily_sink);
auto hw_logger = std::make_shared<spdlog::logger>("hw", daily_sink);
auto db_logger = std::make_shared<spdlog::logger>("db", daily_sink);
net_logger->set_level(spdlog::level::critical); // independent levels
hw_logger->set_level(spdlog::level::debug);
// globally register the loggers so the can be accessed using spdlog::get(logger_name)
// 手动注册
spdlog::register_logger(net_logger);
}
catch (const spdlog::spdlog_ex& ex)
{
std::cout << "Log initialization failed: " << ex.what() << std::endl;
}
}