spdlog日志库
·
介绍
spdlog是一个开源的、快速的、仅有头文件的C++日志库。它提供了向stream、标准输出、文件、系统日志、调试器等目标输出日志的能力,支持的平台包括Windows、Linux、Mac、Android。
spdlog中各对象都分为多线程和单线程版本:
- *_st:单线程版本,不用加锁,效率更高;
- *_mt:多线程版本,用于多线程程序下是线程安全的;
基本结构
spdlog
整体结构可分为三层,自下而上分别是sink
、logger
和logger registry
。
sink
执行具体的动作,如写入日志文件或者输出到命令行;logger
,用户调用logger的方法,执行相关动作,logger会调用自身的所有sink进行输出;registry
管理所有的logger,用户创建的logger会注册到registry统一管理。可以通过spdlog::get()访问已创建的logger;
日志记录槽sink
spdlog定义了几种sink,用于不同场景下日志输出,具体包括:
- 生成日志文件;
- 控制台日志输出(支持颜色);
- 支持设置日志文件占用的最大空间,以及最多包含多少个;
- 支持每天生成日志文件;
日志记录器logger
一个logger对象中存储有多个sink,当调用logger的日志输出函数时,logger会调用自身存储的所有sink对象进行输出。logger中主要包含如下的方法:
set_pattern(const std::string&):
置logger包含的所有sink日志输出内容格式;set_level(level_num):
设置logger日志输出最低等级;log(level_num level, log_msg content):
按照level等级输出content;trace(content, arg1, arg2...):
按照trace等级进行输出,输出内容由content与后面的参数格式化而成;
日志级别
spdlog
默认支持七种日志级别,具体如下:
trace
debug
info
warn
err
critical
off
设置输出格式pattern
通过set_pattern
可设定日志格式,如set_pattern("[%Y-%m-%d %H:%M:%S.%e]%l: %v")
。具体内容可以查看链接。
基本使用
各种类型的logger
- 使用默认的logger
//Use the default logger (stdout, multi-threaded, colored)
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::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");
- 控制台输出logger
#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)");
}
- 基本文件logger
#include "spdlog/sinks/basic_file_sink.h"
void basic_logfile_example()
{
try
{
auto logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
}
catch (const spdlog::spdlog_ex &ex)
{
std::cout << "Log init failed: " << ex.what() << std::endl;
}
}
- 循环文件logger
#include "spdlog/sinks/rotating_file_sink.h"
void rotating_example()
{
// Create a file rotating logger with 5mb size max and 3 rotated files
auto 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
#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 logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
}
回溯日志
logger->enable_backtrace(32); // Store the latest 32 messages in a buffer. Older messages will be dropped.
// or my_logger->enable_backtrace(32)..
for(int i = 0; i < 100; i++)
{
logger->debug("Backtrace message {}", i); // not logged yet..
}
// e.g. if some error happened:
logger->dump_backtrace(); // log them now! show the last 32 messages
[2023-04-12 01:40:25.010] [daily_logger] [info] ****************** Backtrace Start ******************
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 68
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 69
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 70
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 71
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 72
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 73
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 74
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 75
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 76
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 77
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 78
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 79
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 80
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 81
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 82
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 83
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 84
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 85
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 86
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 87
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 88
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 89
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 90
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 91
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 92
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 93
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 94
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 95
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 96
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 97
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 98
[2023-04-12 01:40:25.010] [daily_logger] [debug] Backtrace message 99
[2023-04-12 01:40:25.010] [daily_logger] [info] ****************** Backtrace End ********************
周期刷新
// periodically flush all *registered* loggers every 3 seconds:
// warning: only use if all your loggers are thread safe ("_mt" loggers)
spdlog::flush_every(std::chrono::seconds(3));
替换默认logger
通过替换默认logger即可在全局范围内使用该logger。
void replace_default_logger_example()
{
auto new_logger = spdlog::basic_logger_mt("new_default_logger", "logs/new-default-log.txt", true);
spdlog::set_default_logger(new_logger);
spdlog::info("new logger log message");
}
添加不同sink到logger中
// create logger with 2 targets with different log levels and formats.
// the console will show only warnings or errors, while the file will log all.
void multi_sink_example()
{
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", {console_sink, file_sink});
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");
}
更多推荐
已为社区贡献5条内容
所有评论(0)