博客
关于我
c++日志工具spdLog
阅读量:398 次
发布时间:2019-03-05

本文共 1873 字,大约阅读时间需要 6 分钟。

spdLog 简单使用示例代码

spdLog 是一款流行的 C++ 日志工具,使用非常简单。以下是基于源码修改的测试代码示例。

#include

#include <spdlog/spdlog.h>#include <spdlog/sinks/stdout_color_sinks.h>

void err_handler_example() {spdlog::set_error_handler([](const std::string &msg) {printf("Custom log error handler, %s\n", msg.c_str());});}

int main(int argc, char argv) {try {// 注册并设置默认错误处理器spdlog::set_default_error_handler([](const std::string &msg) {printf("**** spdlog 错误处理器, %s *****\n", msg.c_str());});

// 创建控制台颜色日志输出器     auto console_sink = std::make_shared
(); console_sink->set_level(spdlog::level::debug); console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S:%e] [%n] [%t] [%l] %v%$"); // 创建旋转文件日志输出器 auto file_sink = std::make_shared
( "logs/multisink.txt", 1048576, 3 ); file_sink->set_level(spdlog::level::debug); file_sink->set_pattern("[%Y-%m-%d %H:%M:%S:%e] [%n] [%t] [%l] %v"); // 创建日志对象,绑定多个输出器 auto logger = std::make_shared
("multi_sink", begin(sinks), end(sinks)); // 设置默认日志级别 logger->set_level(spdlog::level::debug); // 自定义错误处理器 logger->set_error_handler([](const std::string &msg) { printf("*****Custom log error handler, %s*****\n", msg.c_str()); }); // 测试输出 logger->warn("这条信息应该同时出现在控制台和文件中"); logger->info("这条信息应该只出现在文件中"); // 注册日志器 spdlog::register_logger(logger); // 获取已注册的日志器 auto test_logger = spdlog::get("multi_sink"); test_logger->info("获取日志器成功"); // 测试字符串格式化 std::string msg = "你好,spdlog!"; logger->warn("spdlog: {}", msg); // 创建旋转日志器 auto rotating_logger = spdlog::rotating_logger_mt("log_rotating", "logs/rotating.txt", 256, 2); for (int i = 0; i < 10; ++i) { rotating_logger->info("{} * {} 等于 {:>10}", i, i, i * i); } } catch (const spdlog::spdlog_ex &ex) { std::cerr << "日志初始化失败" << ex.what() << std::endl; }

}

转载地址:http://ahgg.baihongyu.com/

你可能感兴趣的文章
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>
Python在def函数中使用for循环
查看>>
Python在Conda环境中,但在Windows虚拟环境中没有激活
查看>>
python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
查看>>
python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
查看>>
Python图像处理:从内存加载jpeg
查看>>
python固定后缀(名物化词汇)词频统计:抽取+统计+可视化
查看>>
python商品评论数据采集与分析可视化系统 Flask框架 requests爬虫 NLP情感分析 毕业设计 源码
查看>>
python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
查看>>
python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
查看>>
Python哪个版本最稳定好用2023.10.19
查看>>
Python和黑客技术的渊源
查看>>
Python和RF编写接口自动化
查看>>
python和js哪个难_【Python】记一次学习,Python 与 js 在实现闭包时的差异
查看>>
python和java哪个更值得学——来自知乎高赞回答
查看>>
python和c混合编程 github_在pycharm中使用git版本管理以及同步github的方法
查看>>
python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
查看>>
Python命名中尾随下划线有什么好处?
查看>>
Python启动脚本
查看>>