Boost.log库使用方法
来源:互联网 发布:防sql注入攻击 编辑:程序博客网 时间:2024/05/23 19:55
做服务器开发首要做的事情,就是将日志系统建立起来。这个系统也是方便我们检查服务器的问题。
初始化日志系统
这是一个初始化日志系统的代码。我这里是使用了一个配置文件来做boost.log的设置的。所以代码写起来是非常的简洁。
#include <fstream>#include <boost/filesystem.hpp>#include <boost/log/trivial.hpp>#include <boost/log/utility/setup/common_attributes.hpp>#include <boost/log/utility/setup/from_stream.hpp>#include <boost/log/utility/setup/formatter_parser.hpp>#include <boost/log/utility/setup/filter_parser.hpp>bool init_log_environment(std::string _cfg){ namespace logging = boost::log; using namespace logging::trivial; if (!boost::filesystem::exists("./log/")) { boost::filesystem::create_directory("./log/"); } logging::add_common_attributes(); logging::register_simple_formatter_factory<severity_level, char>("Severity"); logging::register_simple_filter_factory<severity_level, char>("Severity"); std::ifstream file(_cfg); try { logging::init_from_stream(file); } catch (const std::exception& e) { std::cout << "init_logger is fail, read log config file fail. curse: " << e.what() << std::endl; exit(-2); } return true;}
其实最核心的代码就是logging::init_from_stream这个函数。他能加载你对日志的配置信息。这样比起一般将sink设置,日志级别设置写到代码里面的方式要更加简单。接下来我们来看看配置文件的写法。
配置日志
[Core]DisableLogging=falseFilter="%Severity% >= trace"[Sinks.console]Filter="%Severity% > trace "Destination=ConsoleFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=falseAutoFlush=true# trace,[Sinks.trace]Filter="%Severity% = trace"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] %Message%"Asynchronous=falseAutoFlush=trueRotationSize= 31457280FileName="./log/trace_%03N.log"# debug,[Sinks.debug]Filter="%Severity% = debug"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=falseAutoFlush=trueRotationTimePoint="00:00:00"FileName="./log/debug_%Y%m%d_%H%M%S.log"# info,[Sinks.info]Filter="%Severity% = info"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=falseAutoFlush=trueRotationTimePoint="00:00:00"FileName="./log/info_%Y%m%d_%H%M%S.log"# warning,[Sinks.warning]Filter="%Severity% = warning"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=falseAutoFlush=trueRotationTimePoint="00:00:00"FileName="./log/warning_%Y%m%d_%H%M%S.log"# error,[Sinks.error]Filter="%Severity% = error"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=falseAutoFlush=trueRotationTimePoint="00:00:00"FileName="./log/erro_%Y%m%d_%H%M%S.log"# fatal[Sinks.fatal]Filter="%Severity% = fatal"Destination=TextFileFormat="[%TimeStamp%] [%Severity%] [%Channel%] %Message%"Asynchronous=trueAutoFlush=trueRotationTimePoint="00:00:00"FileName="./log/fatal_%Y%m%d_%H%M%S.log"
在这个配置文件中,我们可以直接定义logging core的日志输出级别。设置每个级别的日志的格式。比较屌的是他这里可以通过配置来让日志翻滚起来。仔细看里面的日志配置,他里面是能支持按照时间来写日志的。
里面其实还能自己建立对于特定的日志做过滤条件来做分析:
Filter="%Severity% = fatal"
参考文档
可以看到不仅仅是能支持RotationTimePoint,我当前例子是用的0点刷新的方式。而且还支持使用间隔时间刷新、文件大小刷新。而且还能指定Rotation文件备份的位置。
如何写日志
头文件中引入
#include <boost/log/trivial.hpp>#include <boost/log/sources/severity_channel_logger.hpp>namespace logging = boost::log;using namespace logging::trivial;namespace src = boost::log::sources;//在自己的class里面定义一个日志输出通道对象src::severity_channel_logger<severity_level, std::string> scl;
cpp文件中的写写法
// 构造函数的时候,初始化日志输出通道对象xxxx::xxx(void) : scl(keywords::channel = "xxxx_class")// 按照日志级别写入日志BOOST_LOG_SEV(scl, debug) << __FUNCTION__ << ":" << __LINE__ << " success, fd: " << fd << ", ip: " << str_addr;
输出结果
[2016-08-25 10:29:18.328592] [debug] [config] config::show_config:53 port:9210[2016-08-25 10:29:18.333095] [debug] [config] config::show_config:54 name:abelserver[2016-08-25 10:29:18.335597] [debug] [config] config::show_config:55 log_config:log_conf.ini[2016-08-25 10:29:18.338599] [debug] [config] config::show_config:56 redis_host:127.0.0.1[2016-08-25 10:29:18.342104] [debug] [config] config::show_config:57 redis_port:6379# 输出文件名称:erro_20160325_145443.log
在做一些调试工作的时候,可能需要将数据dump出来看,就可以使用:
void on_receive(std::vector< unsigned char > const& packet){ // Outputs something like "Packet received: 00 01 02 0a 0b 0c" BOOST_LOG(lg) << "Packet received: " << logging::dump(packet.data(), packet.size());}
使用起来还是非常简单的。而且已经满足了大多数的需求。boost.log库是能支持自定义的sink,具体可以需要查阅这里
0 0
- Boost.log库使用方法
- Boost log库
- boost log库
- boost log库
- boost log库
- boost log库使用一
- boost log库使用二
- boost log库使用三
- boost log库使用四
- boost log库使用五
- boost log库使用六
- boost log库 使用七
- boost log库 使用十
- boost log库 使用八
- boost log库 使用九
- boost log库 使用十一
- boost::log库,示例代码
- Boost log库的使用
- c# 入门级Java对比 3 —— 类的继承
- HDU 5443 The Water Problem(ST)
- 读《About Face 4 交互设计精髓》12
- MyEclipse安装JS代码提示(Spket插件)
- android书籍
- Boost.log库使用方法
- Android中Layer-list的使用 为控件添加边线
- PHP中操作MongoDB
- RSA公钥,私钥的生成与配置
- Xamarin.iOS模拟器调试找不到资源文件
- 顺序表应用3:元素位置互换之移位算法
- 数据库视图view简介
- 设计模式之禅——代理模式(一)普通代理&强制代理&虚拟代理&动态代理
- Swift 枚举