This time, I will try filter. first code from official doc,
When run the binary, output is changed, trac and debug level info were filtered out.
But could we change the filter in runtime. Let's try. Change the code now:
It works. That means we can change the log filter without redeploying app. In my TCP server, it can listen to another port for admin usage and receive special message for changing log filter.
Good feature! I like this.
#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";
}
When run the binary, output is changed, trac and debug level info were filtered out.
$ ./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
But could we change the filter in runtime. Let's try. Change the code now:
#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";
}
Ok, watch the result now.
$ ./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
It works. That means we can change the log filter without redeploying app. In my TCP server, it can listen to another port for admin usage and receive special message for changing log filter.
Good feature! I like this.
No comments:
Post a Comment