boost log库使用二

6421人阅读 评论(0) 收藏 举报
分类:

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

#include <iostream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
namespace logging = boost::log;
using namespace std;
void SetFilter() {
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
}
int main () {
  cout << "hello, world" << endl;
  SetFilter();
  BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
  BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
  BOOST_LOG_TRIVIAL(info) << "An informational severity message";
  BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
  BOOST_LOG_TRIVIAL(error) << "An error severity message";
  BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}

当运行程序后,输出结果变了,trac和debug级别的信息被过滤掉了。

$ ./main
hello, world
[2013-12-03 15:34:47.343223] [0x000007f680e76774] [info]    An informational severity message
[2013-12-03 15:34:47.343324] [0x000007f680e76774] [warning] A warning severity message
[2013-12-03 15:34:47.343341] [0x000007f680e76774] [error]   An error severity message
[2013-12-03 15:34:47.343356] [0x000007f680e76774] [fatal]   A fatal severity message
但是如果我们在运行时改变这个filter的设置,会怎样?现在试试看,修改一下代码:

#include <iostream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
namespace logging = boost::log;
using namespace std;
void SetFilter1() {
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
}
void SetFilter2() {
  logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);
}
int main () {
  cout << "hello, world" << endl;
  SetFilter1();
  BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
  BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
  BOOST_LOG_TRIVIAL(info) << "An informational severity message";
  BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
  BOOST_LOG_TRIVIAL(error) << "An error severity message";
  BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
 
  cout << "--------------------" << endl;
  SetFilter2();
  BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
  BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
  BOOST_LOG_TRIVIAL(info) << "An informational severity message";
  BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
  BOOST_LOG_TRIVIAL(error) << "An error severity message";
  BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
}
好,现在观察结果。

$ ./main
hello, world
[2013-12-03 15:37:54.399513] [0x000007fd7709a374] [info]    An informational severity message
[2013-12-03 15:37:54.399612] [0x000007fd7709a374] [warning] A warning severity message
[2013-12-03 15:37:54.399630] [0x000007fd7709a374] [error]   An error severity message
[2013-12-03 15:37:54.399644] [0x000007fd7709a374] [fatal]   A fatal severity message
--------------------
[2013-12-03 15:37:54.399666] [0x000007fd7709a374] [debug]   A debug severity message
[2013-12-03 15:37:54.399680] [0x000007fd7709a374] [info]    An informational severity message
[2013-12-03 15:37:54.399693] [0x000007fd7709a374] [warning] A warning severity message
[2013-12-03 15:37:54.399706] [0x000007fd7709a374] [error]   An error severity message
[2013-12-03 15:37:54.399719] [0x000007fd7709a374] [fatal]   A fatal severity message

起作用了。也就是说我们可以在不重新部署程序的情况下修改日志的过滤级别。在我的TCP服务中,可以监听一个管理端口,专门用来接受用于改变过滤级别的特殊消息。

非常好的特性,我喜欢这个功能。



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5261655次
    • 积分:64821
    • 等级:
    • 排名:第29名
    • 原创:1203篇
    • 转载:22篇
    • 译文:1篇
    • 评论:1140条
    文章分类
    最新评论
    算法专家朋友的博客
    清如许的博客