Before boost::log
made it into the official boost library (as of 1.54), I was using the sourceforge version with the following code:
boost::log::formatters::fmt_format<char> simpleFormat(
boost::log::formatters::format("%1% %2%") %
boost::log::formatters::date_time<boost::posix_time::ptime>(
"TimeStamp", boost::log::keywords::format = "%H:%M:%S") %
boost::log::formatters::message());
and the later on:
log_sink->locked_backend()->set_formatter(simpleFormat)
However, I simply cannot figure out how to express the above logging format with the new boost::log
API without using stream syntax (I want to stick to the formatting style syntax as above). Any ideas?
Update: This is the compiler error I get (using gcc 4.6.3):
> boost/boost/log/expressions/formatters/date_time.hpp: In constructor
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::format_date_time_terminal(const
> boost::log::v2s_mt_posix::attribute_name&, const fallback_policy&,
> const string_type&) [with T = boost::posix_time::ptime,
> FallbackPolicyT = boost::log::v2s_mt_posix::fallback_to_none, CharT =
> char,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::fallback_policy =
> boost::log::v2s_mt_posix::fallback_to_none,
> boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T,
> FallbackPolicyT, CharT>::string_type = std::basic_string<char>]’:
> /boost/boost/log/expressions/formatters/date_time.hpp:229:94:
> instantiated from
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_actor<AttributeValueT,
> boost::log::v2s_mt_posix::fallback_to_none, CharT>
> boost::log::v2s_mt_posix::expressions::format_date_time(const
> boost::log::v2s_mt_posix::attribute_name&, const CharT*) [with
> AttributeValueT = boost::posix_time::ptime, CharT = char]’
> /src/log.cc:169:102: instantiated from here
> /boost/boost/log/expressions/formatters/date_time.hpp:94:98: error:
> incomplete type
> ‘boost::log::v2s_mt_posix::expressions::format_date_time_terminal<boost::posix_time::ptime,
> boost::log::v2s_mt_posix::fallback_to_none, char>::formatter_generator
> {aka
> boost::log::v2s_mt_posix::expressions::aux::date_time_formatter_generator_traits<boost::posix_time::ptime,
> char, void>}’ used in nested name specifier compilation terminated due
> to -Wfatal-errors.
Update 2: I had to include
#include <boost/log/support/date_time.hpp>
in order to make it work.
Is that what you want?
http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/tutorial/formatters.html#log.tutorial.formatters.boost_format_style_formatters
Example for your case
logging::formatter simpleFormat(expr::format("%1% %2%") %
expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S") %
expr::smessage);
You can also format the log like this by using this code :
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
logging::add_file_log
(
keywords::file_name = "Sample_%N.log", /*< file name pattern >*/
keywords::rotation_size = 1 * 1024, /*< rotate files every 1 KiB >*/
/*< log record format >*/
keywords::format =
(
expr::stream
//<< std::hex //To print the LineID in Hexadecimal format
<< std::setw(8) << std::setfill('0')
<< expr::attr< unsigned int >("LineID")
<< "\t"
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp","%H:%M:%S.%f")
<< "\t: <" << logging::trivial::severity
<< "> \t" << expr::smessage
)
);
Output is like :
00000741 15:50:11.636997 : <> Logger Starts
00000742 15:50:11.660012 : <> Thread74
00000743 15:50:11.682028 : <trace> Trace message new
00000744 15:50:11.703041 : <debug> Debug Message new
00000745 15:50:11.725055 : <info> Info message
00000746 15:50:11.748088 : <warning> warning Warning message new
00000747 15:50:11.770083 : <error> error An error message new
00000748 15:50:11.792100 : <fatal> fatal A fatal message new
00000749 15:50:11.818117 : <> Thread74
00000750 15:50:11.841148 : <> Logger Stops
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With