So, you don't have time to spend (at least not right now), to read the tutorial, but still want to use the Boost Logging Lib.
There are several writers. The most common uses the concept of formatters and destinations:
Once you have a writer, you can add several formatters and/or destinations. The writer object knows how to call them. The common (and default) one calls them like this:
The easiest writer is the Named Writer. It's an easy interface to using formatters and destinations. In other words, you set a format string and a destination string.
You should structure your code like this:
#include
the Boost Logging Lib forward classes, and you declare your loggers and filters
// my_app_log.h - DECLARE your loggers & filters here #ifndef my_app_LOG_H_header #define my_app_LOG_H_header #include <boost/logging/format/named_write_fwd.hpp> // #include <boost/logging/writer/on_dedicated_thread.hpp> // uncomment if you want to do logging on a dedicated thread namespace bl = boost::logging; typedef bl::named_logger<>::type logger_type; typedef bl::filter::no_ts filter_type; BOOST_DECLARE_LOG_FILTER(g_l_filter, filter_type) BOOST_DECLARE_LOG(g_l, logger_type) #define L_ BOOST_LOG_USE_LOG_IF_FILTER( g_l(), g_log_filter()->is_enabled() ) // initialize thy logs.. void init_logs(); #endif // my_app_log.cpp - DEFINE your loggers & filters here #include "my_app_log.h" #include <boost/logging/format/named_write.hpp> BOOST_DEFINE_LOG_FILTER(g_log_filter, filter_type ) BOOST_DEFINE_LOG(g_l, logger_type) void init_logs() { // formatting : time [idx] message \n // destinations : console, file "out.txt" and debug window g_l()->writer().write("%time%($hh:$mm.$ss.$mili) [%idx%] |\n", "cout file(out.txt) debug"); g_l()->mark_as_initialized(); } void use_logger() { int i = 1; L_ << "this is a simple message " << i; std::string hello = "hello"; L_ << hello << " world"; }
// my_app_log.h - DECLARE your loggers & filters here #ifndef my_app_LOG_H_header #define my_app_LOG_H_header #include <boost/logging/format/named_write_fwd.hpp> // #include <boost/logging/writer/on_dedicated_thread.hpp> // uncomment if you want to do logging on a dedicated thread namespace bl = boost::logging; typedef bl::named_logger<>::type logger_type; typedef bl::level::holder filter_type; BOOST_DECLARE_LOG_FILTER(g_l_level, filter_type) BOOST_DECLARE_LOG(g_l, logger_type) #define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_l_level(), lvl ) // initialize thy logs.. void init_logs(); #endif // my_app_log.cpp - DEFINE your loggers & filters here #include "my_app_log.h" #include <boost/logging/format/named_write.hpp> BOOST_DEFINE_LOG_FILTER(g_l_level, filter_type ) BOOST_DEFINE_LOG(g_l, logger_type) void init_logs() { // formatting : time [idx] message \n // destinations : console, file "out.txt" and debug window g_l()->writer().write("%time%($hh:$mm.$ss.$mili) [%idx%] |\n", "cout file(out.txt) debug"); g_l()->mark_as_initialized(); } void use_logger() { int i = 1; L_(debug) << "this is a simple message " << i; std::string hello = "hello"; L_(info) << hello << " world"; }