boost log库

这次,尝试filter,首先代码来自于官方文档。

  1. #include <iostream>  
  2. #include <boost/log/core.hpp>  
  3. #include <boost/log/trivial.hpp>  
  4. #include <boost/log/expressions.hpp>  
  5. namespace logging = boost::log;  
  6. using namespace std;  
  7. void SetFilter() {  
  8.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);  
  9. }  
  10. int main () {  
  11.   cout << "hello, world" << endl;  
  12.   SetFilter();  
  13.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  14.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  15.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  16.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  17.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  18.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  19. }  

当运行程序后,输出结果变了,trac和debug级别的信息被过滤掉了。
  1. $ ./main  
  2. hello, world  
  3. [2013-12-03 15:34:47.343223] [0x000007f680e76774] [info]    An informational severity message  
  4. [2013-12-03 15:34:47.343324] [0x000007f680e76774] [warning] A warning severity message  
  5. [2013-12-03 15:34:47.343341] [0x000007f680e76774] [error]   An error severity message  
  6. [2013-12-03 15:34:47.343356] [0x000007f680e76774] [fatal]   A fatal severity message  
但是如果我们在运行时改变这个filter的设置,会怎样?现在试试看,修改一下代码:
  1. #include <iostream>  
  2. #include <boost/log/core.hpp>  
  3. #include <boost/log/trivial.hpp>  
  4. #include <boost/log/expressions.hpp>  
  5. namespace logging = boost::log;  
  6. using namespace std;  
  7. void SetFilter1() {  
  8.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);  
  9. }  
  10. void SetFilter2() {  
  11.   logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);  
  12. }  
  13. int main () {  
  14.   cout << "hello, world" << endl;  
  15.   SetFilter1();  
  16.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  17.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  18.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  19.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  20.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  21.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  22.    
  23.   cout << "--------------------" << endl;  
  24.   SetFilter2();  
  25.   BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  26.   BOOST_LOG_TRIVIAL(debug) << "A debug severity message";  
  27.   BOOST_LOG_TRIVIAL(info) << "An informational severity message";  
  28.   BOOST_LOG_TRIVIAL(warning) << "A warning severity message";  
  29.   BOOST_LOG_TRIVIAL(error) << "An error severity message";  
  30.   BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";  
  31. }  
好,现在观察结果。
  1. $ ./main  
  2. hello, world  
  3. [2013-12-03 15:37:54.399513] [0x000007fd7709a374] [info]    An informational severity message  
  4. [2013-12-03 15:37:54.399612] [0x000007fd7709a374] [warning] A warning severity message  
  5. [2013-12-03 15:37:54.399630] [0x000007fd7709a374] [error]   An error severity message  
  6. [2013-12-03 15:37:54.399644] [0x000007fd7709a374] [fatal]   A fatal severity message  
  7. --------------------  
  8. [2013-12-03 15:37:54.399666] [0x000007fd7709a374] [debug]   A debug severity message  
  9. [2013-12-03 15:37:54.399680] [0x000007fd7709a374] [info]    An informational severity message  
  10. [2013-12-03 15:37:54.399693] [0x000007fd7709a374] [warning] A warning severity message  
  11. [2013-12-03 15:37:54.399706] [0x000007fd7709a374] [error]   An error severity message  
  12. [2013-12-03 15:37:54.399719] [0x000007fd7709a374] [fatal]   A fatal severity message  

起作用了。也就是说我们可以在不重新部署程序的情况下修改日志的过滤级别。在我的TCP服务中,可以监听一个管理端口,专门用来接受用于改变过滤级别的特殊消息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值