In this example, all output will be written to the console, debug output window, and "out.txt" file. It will look similar to this one:
00001 21:03.17.243 [1] this is so cool 1 00002 21:03.17.243 [2] first error 2 00003 21:03.17.243 [3] hello, world 00004 21:03.17.243 [4] second error 3 00005 21:03.17.243 [5] good to be back ;) 4 00006 21:03.17.243 [6] third error 5
00001 00030 #include <boost/logging/format/named_write.hpp> 00031 typedef boost::logging::named_logger<>::type logger_type; 00032 00033 #define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), lvl ) 00034 00035 BOOST_DEFINE_LOG_FILTER(g_log_level, boost::logging::level::holder ) // holds the application log level 00036 BOOST_DEFINE_LOG(g_l, logger_type) 00037 00038 void test_mul_levels_one_logger() { 00039 // formatting : time [idx] message \n 00040 // destinations : console, file "out.txt" and debug window 00041 g_l()->writer().write("%time%($hh:$mm.$ss.$mili) [%idx%] |\n", "cout file(out.txt) debug"); 00042 g_l()->mark_as_initialized(); 00043 00044 int i = 1; 00045 L_(debug) << "this is so cool " << i++; 00046 L_(error) << "first error " << i++; 00047 00048 std::string hello = "hello", world = "world"; 00049 L_(debug) << hello << ", " << world; 00050 00051 using namespace boost::logging; 00052 g_log_level()->set_enabled(level::error); 00053 L_(debug) << "this will not be written anywhere"; 00054 L_(info) << "this won't be written anywhere either"; 00055 L_(error) << "second error " << i++; 00056 00057 g_log_level()->set_enabled(level::info); 00058 L_(info) << "good to be back ;) " << i++; 00059 L_(error) << "third error " << i++; 00060 } 00061 00062 00063 00064 int main() { 00065 test_mul_levels_one_logger(); 00066 } 00067 00068 00069 // End of file 00070