博客
关于我
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--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>
Python-numpy(2)数组的操作
查看>>
Python-Numpy的tile函数用法
查看>>
python-open函数
查看>>
Python-Pandas-将特定函数应用于给定级别-多索引数据框架
查看>>
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>
Python再获年度编程语言,微软或成最大赢家
查看>>
python-week5
查看>>
Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
Python-【1】配置
查看>>