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

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

c++日志工具spdLog简单使用示例代码

spdlog直接引用头文件就可以使用,这一点还是比较方便的,也是刚入门使用,下面是在源码的示例代码基础上修改测试的代码:

#include 
#include
#include "spdlog/spdlog.h"#include "spdlog/sinks/stdout_color_sinks.h" // or "../stdout_sinks.h" if no color needed#include "spdlog/sinks/basic_file_sink.h"#include "spdlog/sinks/rotating_file_sink.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 , char *[]){ try { 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", false); auto file_sink = std::make_shared
("logs/multisink.txt", 1048576, 3); file_sink->set_pattern("[%Y-%m-%d %H:%M:%S:%e] [%n] [%t] [%l] %v"); file_sink->set_level(spdlog::level::debug); std::vector
sinks; sinks.push_back(console_sink); sinks.push_back(file_sink); //spdlog::logger *logger = new spdlog::logger("multi_sink", {console_sink, file_sink}); auto logger = std::make_shared
("multi_sink", begin( sinks ), end( sinks )); //logger->set_level(spdlog::level::debug);#if 0 //也可以使用这种方式 spdlog::sinks_init_list sink_list = { console_sink->sinks().front(), file_sink->sinks().front() }; //创建一个新的日志对象,以上面两个日志对象作为初始化参数,即实现了同时输出 console 和 file auto log_ptr = spdlog::create("loggername", sink_list); spdlog::register_logger( combined_logger );#endif err_handler_example(); logger->warn("this should appear in both console and file"); logger->info("this message should appear in file , not in console"); //注册到spdlog里 spdlog::register_logger(logger); auto test_logger = spdlog::get("multi_sink"); test_logger->info("getlogger::helloworld"); std::string msg = "hello world 2019."; 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("{} * {} equals {:>10}", i, i, i*i); } catch (const spdlog::spdlog_ex &ex) { std::cout<<"Log initialization faild"<
<

 

个人微信服务号同步推送文章(微信公众号:fensTeck):

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

你可能感兴趣的文章
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>