c++ 日志输出库 spdlog 简介(1)

时间:2023-12-28 10:49:44

参考文章:

log库spdlog简介及使用 - 网络资源是无限的 - CSDN博客 http://blog.csdn.net/fengbingchun/article/details/78347105

spdLog的使用 - 烟消bug云散的专栏 - CSDN博客 http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533

官方参考文档: QuickStart · gabime/spdlog Wiki · GitHub

https://github.com/gabime/spdlog/wiki/1.-QuickStart

1、下载源码

代码地址在 https://github.com/gabime/spdlog

c++ 日志输出库 spdlog 简介(1)

点击downLoad下载即可。

2、example解析

下载压缩包并解压:使用visual studio 打开vcxproj后缀的项目文件(我用的是VS2013)

c++ 日志输出库 spdlog 简介(1)

在解决方案中找到example.cpp,这个源文件例举了spdlog的各种用法:

首先需要包含spdlog的头文件

#include "spdlog/spdlog.h"

并且要声明spdlog的命名空间

namespace spd = spdlog;

(1)控制台(console)输出日志

使用控制台输出日志的话,需要这两个头文件:

#include <iostream>
#include <memory>

代码如下:

// Console logger with color
auto console = spd::stdout_color_mt("console");
console->info("Welcome to spdlog!");
console->error("Some error message with arg{}..", ); // Formatting examples
console->warn("Easy padding in numbers like {:08d}", );
console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", );
console->info("Support for floats {:03.2f}", 1.23456);
console->info("Positional args are {1} {0}..", "too", "supported");
console->info("{:<30}", "left aligned"); spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");

auto console = spd::stdout_color_mt("console"); 中“console”为logger名称,可以随意命名。

warn,critical,info 为不同等级的log,输出在控制台会以不同颜色表示。

c++ 日志输出库 spdlog 简介(1)

注意,logger使用完,程序关闭之前需要调用drop函数释放logger对象,否则如果程序没有关闭,就无法再建立同样名称的logger。

在example.cpp中main函数的最后调用了

// Release and close all loggers
spdlog::drop_all();

如果只想关闭console的log,可以这样写:

spd::drop("basic_logger");

(2)basic log

不带滚动,日志文件会一直被写入,不断变大。

// Create basic file logger (not rotated)
auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt");
my_logger->info("Some log message");

(3)rotating log

滚动日志,当日志文件超出规定大小时,会删除当前日志文件中所有内容,重新开始写入。

从函数声明可以看出,参数max_file_size 规定了文件的最大值,文件内容超过此值就会清空。

rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)

参数max_files 规定了滚动文件的个数。当logger_name存满时,将其名称更改为logger_name.1,再新建一个logger_name文件来存储新的日志。再次存满时,把logger_name.1改名为logger_name.2,logger_name改名为logger_name.1,新建一个logger_name来存放新的日志。max_files 数量为几,就可以有几个logger_name文件用来滚动。

下面的例子运行后生成了三个log文件。

// Create a file rotating logger with 5mb size max and 3 rotated files
//auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", , );
for (int i = ; i < ; ++i)
rotating_logger->info("{} * {} equals {:>10}", i, i, i*i);

c++ 日志输出库 spdlog 简介(1)

每个文件内容如下,后缀数字越大,日志内容越早:

c++ 日志输出库 spdlog 简介(1)

c++ 日志输出库 spdlog 简介(1)

c++ 日志输出库 spdlog 简介(1)

(4)daily log

每天会新建一个日志文件,新建日志文件的时间可自己设定。

// Create a daily logger - a new file is created every day on 2:30am
auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", , );
// trigger flush if the log severity is error or higher
daily_logger->flush_on(spd::level::err);
daily_logger->info(123.44);

上述代码输出的日志,如果程序不退出的话,就是每天2:30 am创建新的文件。如果一天多次运行这个程序,就会有多个日志文件,如下图:

c++ 日志输出库 spdlog 简介(1)

为了把每天的log写到同一个文件中去,参考http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533

可以这样写:

//创建文件名类似于: log_2018-01-17.txt
typedef spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::dateonly_daily_file_name_calculator> dateonly_daily_file_sink_mt;
auto m_logger = spdlog::create<dateonly_daily_file_sink_mt>("m_logger", "logs/dateonly.txt", , );
m_logger->info("test daily info");
m_logger->error("test daily error");

(5)flush 将buffer刷入文件

遇到指定级别的日志会立马将缓存输出到文件中,如果不立刻写入,当程序发生崩溃或产生异常而退出时,有些重要log可能还没等写入到文件中。日志的各个级别如下面代码所示:

typedef enum
{
trace = ,
debug = ,
info = ,
warn = ,
err = ,
critical = ,
off =
} level_enum;
// trigger flush if the log severity is error or higher

        daily_logger->flush_on(spd::level::err);
daily_logger->info(123.44);
daily_logger->error("Error happended! ");

基本的日志输出大概这样,下一篇继续。